Skip to content

Latest commit

 

History

History
168 lines (135 loc) · 3.63 KB

2.8-Collections: Set Map and WeakMap.md

File metadata and controls

168 lines (135 loc) · 3.63 KB

2.8 - Collections: Set(), Map() and WeakMap(): set, get, delete

ecma-international.org

Set()

The Set object lets you store unique values of any type, whether primitive values or object references.

var set = new Set();
set.size; // 0
var set = new Set();
set.add("someKey");
set.size; // 1
var set = new Set();
var key = "someKey", key2 = "someOtherKey";
set.add(key);
set.add(key2); 
set.has("someKey"); // true
set.has("someOtherKey"); // true
set.size; // 2
set.keys(); // {"someKey", "someOtherKey"}
var set = new Set();
var key = "someKey", key2 = "someOtherKey";
set.add(key);
set.add(key2); 
set.has("someKey"); // true
set.has("someOtherKey"); // true
set.size; // 2
set.delete("someKey");
set.size; // 1 
// initialised with an Array
var set2 = new Set([1,2,3]);
set2.has(1); // true
set2.size; // 3
var m = new Map();

m.set("n", "nn");
console.log( m.size ); // 1

m.set("n1", "nn1");
console.log( m.size ); // 2

console.log( m.get("n") ); // nn
console.log( m.has("n1") ); // true
console.log( m.delete("n1") ); // true
console.log( m.has("n1") ); // false

m.forEach(function(value, key){ console.log( key + ' maps to ' + value) }) // n maps to nn

Ej1: Map

// passing values from one Set to other
var set = new Set();
set.add("1");
set.add("2");
set.add("3");

var set2 = new Set(set);
set2.size; // 3

Map()

The Map object is a simple key/value pairs. similar to Object but designed to keep key/value pairs. Any value (both objects and primitive values) may be used as either a key or a value.

// assigning Object to Map
var map = new Map();
var n = {1:1, 2:2};
map.set(n); // Map {Object {1: 1, 2: 2} => undefined}
// size of the Map
var map = new Map();
map.size; // 1
// validate you have an object
var map = new Map();
var test = {"Leo": "Lanese"};
map.set(test);
map.has(test); // true
// Multiple arrays
var map = new Map([[1:1], [2:2], [3,3]]);
map.size; //3
// validate if has a key
var map = new Map([[1,1], [2,2], [3,3]]);
map.has(1); // true
map.has(13); // false
// validate if has a key
var i = 0;
var map = new Map( [[1,1], [2,2], [3,3]] );
map.forEach(function(value,key){
  i++;
  console.log(value , key)
  // do something with value and key
});
// iterators
var i = 0;
var map = new Map([[1,1], [2,2], [3,3]]);
map.set(1, "a")
var keys = map.keys();
var test = keys.next().value; // {1 => "a", 2 => 2, 3 => 3}
console.log(test); // 1
// Map - forEach
var map = new Map([[1,1], [2,2], [3,3]]);
var i;
map.forEach((value,key)=>{
  i++;
  console.log(value, key); 1 1 2 2 3 3
}

Diference between Objects and Maps

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key.

  • An Object has a prototype
  • The keys of an Object are Strings and Symbols
  • You can get the size of a Map easily with the size property

WeakMap

  • Similar to Map
  • The word "weak" refers to weak references. A weak reference is an object reference that is ignored by the garbage collector.
  • Keys must be objetcs
  • keys are garbage-collected (usefull to prevent memory leaks)
  • Can't be iterated
  • Cannot get the size of a WeakMap
  • Cannot iterate over it's keys or values
  • WeakSet has no size property, or a way of iterating its members (references are weak so can be destroyes without notice)