Home Blog About Contact

ES6 Data Structures

Tue, Jul 26, 16

ES6 introduces many new features to embrace the developer as well as the browsers to perform in a optimal performant way.

Set

Set is new type of data structure that we can use to store Unique values which can be of any type but they must all be uniqie.

Create a Set

To create a set we use Set constructor var mySet = new Set();

Add Values to set

let myArray = [1, 4, 1, 5, 5, 7, 8, 9, 0, 0]
let mySet = new Set(myArray)

Now if you do a console.log(mySet); you will see Set {1, 4, 5, 7, 8, 9, 0}

Map

Map is similar to Set but it manges key-value pair instead of individual values.

Create a Map

var myMap = new Map(); will create an instance of Map and to add values to it we can do let myOtherMap = new Map([['a', 2], [1, 'b']])

console.log(myOtherMap); will show Map {"a" => 2, 1 => "b"}

WeakSet and WeakMap

Similar to regular Map and Set but the difference is that the object references inside them are held weakly. So it wont prevent garbage collection.

Full Version of ES6 Data Structures