Created
June 18, 2015 12:22
-
-
Save afonsomatos/3f9f377b609b85f63914 to your computer and use it in GitHub Desktop.
New Collection data structures and methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <---------------> Array methods <-------------------> | |
// Array.from(arrayLike or iterable, mapFunc?, thisArg?) | |
Array.from("hello", c => c.toUpperCase()); | |
// Array.of(...items) | |
Array.of(...'hello'); | |
// -- Array prototype methods | |
// Array.prototype.keys() => Returns iterator of keys | |
['a', 'b', 'c'].keys(); // 0.. 1.. 2.. | |
// Array.prototype.values() => Returns iterator of values | |
['a', 'b', 'c'].values(); // 'a'.. 'b'.. 'c'.. | |
// Array.prototype.entries() => | |
// Returns iterator of arrays [key, value] | |
['a', 'b', 'c'].entries(); // [0, 'a'].. [1, 'b'].. [2, 'c'] | |
// Array.prototype.find(predicate, thisArg?) => Returns the | |
// first value for which the callBack predicatereturns true | |
[-3, -1, 1].find(x => x > 0); // 1 | |
[42, 69, 137].find(x => x < 0); // undefined | |
// Array.prototype.findIndex(predicate, thisArg?) | |
[1, 2, 3].findIndex(x => x % 2 == 0); // 1 (value : 2) | |
// Same as | |
let arr = [1, 2, 3]; | |
arr.indexOf(arr.find(x => x % 2 == 0)); | |
// Array.prototype.fill(value, start?, end?) => | |
// fills an array with the given value | |
new Array(3).fill(1).fill(7, 1, 2); // [1, 7, 1] | |
// <---------------------> Map <----------------------> | |
// Structure to map values to values | |
let map = new Map(); | |
map.set('foo', true); | |
map.set('bar', false); | |
map.size; // 2 | |
map.clear(); | |
map.size; // 0 | |
// Set up a map via an iterable over key-value "pairs" | |
let map = new Map([ | |
[1, 'one'], | |
[2, 'two'], | |
[3, 'three'] | |
]); | |
// Set command is chainable | |
let map = new Map() | |
.set(1, 'one') | |
.set(2, 'two') | |
.set(3, 'three'); | |
// Keys can be anything | |
new Map.get('jfioajsdpdjfidiofsda'); // undefined, not found | |
// Map.prototype.values() <<<< | |
// Map.prototype.keys() <<<<<<<< Work just like arrays | |
// Map.prototype.entries() <<<< | |
// Default iterating over map is entries | |
// so no need to type map.entries() for iterating | |
for (let [key, value] of map); | |
// Map.prototype.forEach((value, key, map) => void, thisArg?) | |
new Map([0, 'a']).forEach(console.log, this); | |
// Single entries handling | |
let map = new Map(); | |
map.set(1, 'dog'); // map | |
map.has(1); // true | |
map.get(1); // 'dog' | |
map.delete(1); // true | |
// All entries handling | |
map.size; // 0 | |
map.clear(); // undefined : remove all entries | |
// <---------------------> WeakMap <----------------------> | |
// Doesn't prevent garbage-collection | |
var map = new Map(); | |
var weakmap = new WeakMap(); | |
(function(){ | |
var a = {x: 12}; | |
var b = {y: 12}; | |
map.set(a, 1); | |
weakmap.set(b, 2); | |
})() | |
// weakmap doesn't have b anymore because its reference | |
// its no longer available | |
// map still has a, because it was stored internally | |
// Same four methods as Maps, but not size or clear | |
WeakMap.prototype.get(key); | |
WeakMap.prototype.set(key, value); | |
WeakMap.prototype.has(key); | |
WeakMap.prototype.delete(key); | |
// <----------------> Set <---------------> | |
// Data structure that handles unique arbitrary values | |
let set = new Set( /* iterable? */ ); | |
// Add value | |
set.add('red').add('blue'); | |
// Has value? | |
set.has('red'); // true | |
// Delete value | |
set.delete('red'); // true | |
// Get size | |
set.size; // 0 | |
// Clear the set | |
set.clear(); | |
// Mapping | |
let set = new Set([1, 2, 3]); | |
set = new Set([...set].map(x => x * 2)); // Set {2, 4, 6} | |
// Filtering | |
let set = new Set([1, 2, 3, 4, 5]); | |
set = new new Set([...set].filter(x => (x % 2) == 0)); // Set {2, 4} | |
// Union | |
let a = new Set([1, 2, 3]); | |
let b = new Set([4, 3, 2]); | |
let union = new Set([...a, ...b]); // {1, 2, 3, 4} | |
// Intersection | |
let intersection = new Set( | |
[...a].filter(x => b.has(x))); // {2, 3} | |
// Difference | |
let difference = new Set( | |
[...a].filter(x => !b.has(x))); // {1} | |
// Iterating and looping | |
Set.prototype.values(); | |
Set.prototype.forEach((value, key, collection) => ); | |
// Only to become similar with Map | |
// return only the values [key = value, value] | |
Set.prototype.entries(); | |
Set.prototype.keys(); | |
// <----------------> WeakSet <---------------> | |
// Same as Sets but don't have .entries, .keys | |
// and prevent its values from being garbage-collected | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment