January 23, 2019

ES6 Map() Object

In Javascript, we have mainly two types to store data for complex data or mixed type of data - Array and Objects. Whereas in the rest of the programming language we have many more data structures. Objects are the most flexible and useful when you want to store the data in a key/value pair. The only issue is that the key can be only String.

Though you can convert the numbers later by using helper methods. But, what if you can save any data types as a key? Would that not be awesome?

Map() do same for us. Map object let us save the data in key/value pair but we can make key as. - string, number, even function too. - map use set to set() the key and value, get() to get the value.

How map() is useful?

If you are looking to save the data in key/value pair where keys could be of any data-type from number to the method then map() is for you. If you are implementing hashmap data structure in javascript then map() is that for you.

Objects vs Map()

Objects can only have keys as the string. Whereas, the map is free to have any data-type. The keys in the object are not ordered in the order they are inserted whereas keys in Map are ordered. The properties in the object needed to be calculated manually whereas in Map it can be done by using size.
The map is very easy to iterate in comparison to object Snapshot of the code:

Here is the code.

No comments: