JavaScript Collections

JavaScript Collections

Originally I was revising JavaScript ended up turning the stuff into a blog :)

Basic arrays

  • Collection of objects of any type

    • Can be a mixed type

      • Numbers, Strings, Objects, Functions
  • Element access

  • Length

  • Holes

  • Iteration

Iteration

  • Go over all the elements in a collection

  • Concepts:

    • Iterable -> An object whose contents can be accessed sequentially

    • Iterator -> Pointer to the next element

  • Iterable Objects:

    • Array

    • String

    • Map

    • Set

    • Browser DOM - tree structure

  • Objects

    •   object.keys()
      
    •   object.entries()
      
    • These are some helper functions

Iterations and Transformations

  • Functions that take functions as input

  •   map, filter, find
      //Apply a callback function over each element of array
    
  • Elements of functional programming

    • Create a transformation programming

Callback -> Function passed in to another function to be called for some purpose

Other Collections

  • Maps -> Proper dictionaries instead of objects

  • WeakMaps

  • Sets

More advanced stuff -> use only when needed

Destructuring

  • Simple syntax to split an array into multiple variables

  • Easier to pass and collect arguments, etc

  • Also possible for objects

Generators

  • Functions that yield values one at a timed

  • Computed iterables

  • Dynamically generate iterators

Basic array

let x = [2, 3, 5, 5];
console.log(`${typeof x}: ${x} with length = ${x.length}`);
console.log(x[0]);

Output: object: 2,3,5,5 with length = 4

2

Mixed element array

let y = [1, 'b', a => a + 1];
console.log(`${typeof(y)}: ${y} with length = ${y.length}`);
console.log(x.length, y.length);

Output: 1, b, a => a + 1 with length = 3

4,3

Holes in the array

This creates holes in the array

We have changed the size of the array without adding any element, so the elements are undefined

y.length = 10;
console.log(`${typeof(y)}: ${y} with length = ${y.length}`);

Did you find this article valuable?

Support Mohammed Asadullah Sayeed by becoming a sponsor. Any amount is appreciated!