Objects and Arrays are the well known data structures in JavaScript. ES2015 introduces new data structures like Set, Map, and WeakMap for efficiently working with collections.
and transversal House Analogy - Numbers, Booleans, and Strings are the bricks - You can’t build a house with a single brick - Collections group these bricks to build complex structures
too hard so use a library like lodash _.uniq([2, 1, 2]); // ES5 solution [2, 1, 2].filter(function (elem, pos, arr) { return arr.indexOf(elem) === pos; });
also unique const mySet = new Set(); const obj = { a: 1, b: 2 }; mySet.add(obj); // obj is referencing a different object so this is okay mySet.add({a: 1, b: 2}); mySet.has(obj); // true
as key/value - Objects are not converted to strings - Use maps when keys are unknown until runtime const myMap = new Map(); // methods myMap.set(1, 'john'); myMap.get(1); myMap.has(1); // => true myMap.has(2); myMap.delete(1); myMap.clear(); // properties myMap.size;
be passed as keys - Other data types are not allowed - Better with Memory - Individual entries can be garbage collected while WeakMap still exists - Cleaned up when no longer referenced anywhere else - Cannot be enumerated