ES6 Data Structures

Tue Jul 26 2016

ES6 Data Structures

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 unique.

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.

This article originally appeared on lightrains.com

Leave a comment

To make a comment, please send an e-mail using the button below. Your e-mail address won't be shared and will be deleted from our records after the comment is published. If you don't want your real name to be credited alongside your comment, please specify the name you would like to use. If you would like your name to link to a specific URL, please share that as well. Thank you.

Comment via email
Nikhil M
Nikhil M

Entrepreneur / Privacy Freak / Humanist / Blockchain / Ethereum / Elixir / Digital Security / Online Privacy

Tags Recent Blogs