JavaScript Map
and Set
objects are both iterable objects. Both store object by [key, value]
pair. I want to know when to use what? Is there any preference one over another?
In Map() , key can be any type [String, number, Object] but in regular object key must be a string type. Set is one dimensional unique array, however Map is 2D and has key-value pair, where key shall be unique.
An object behaves like a dictionary because JavaScript is dynamically typed, allowing you to add or remove properties at any time. But Map() is much better because it: Provides get , set , has , and delete methods. Accepts any type for the keys instead of just strings.
In Object, the data-type of the key-field is restricted to integer, strings, and symbols. Whereas in Map, the key-field can be of any data-type (integer, an array, even an object!) In the Map, the original order of elements is preserved.
Object is the great choice for scenarios when we only need simple structure to store data and knew that all the keys are either strings or integers (or Symbol), because creating plain Object and accessing Object's property with a specific key is much faster than creating a Map (literal vs constructor, direct vs get() ...
Provided you are talking about the ES6 types, they aren't the same data structure even though the Set
might be implemented with a Map
.
Your definition of Map
is right, but a Set
is a collection of unique values, unlike an array which can have duplicates.
var array = [1, 2, 3, 3]; var set = new Set(array); // Will have [1, 2, 3] assert(set.size, 3); var map = new Map(); map.set('a', 1); map.set('b', 2); map.set('c', 3); map.set('C', 3); map.set('a', 4); // Has: a, 4; b, 2; c: 3, C: 3 assert(map.size, 4);
Set
when your dataset needs to be composed of unique valuesMap
when you have pairs of associated data. You map the keys to the valuesSet
:There is a meeting with people coming from different organizations. Some people come from the same organization. We need to compose a list all the different organzations. For this we can use a set since we only want to include every organization once:
const organization = new Set(); organization.add('org1'); organization.add('org2'); organization.add('org3'); organization.add('org1'); organization.add('org3'); organization.add('org1'); for(let org of organization){ console.log(org); }
Map
:We have a pack of dogs and want to assign an age to each dog. We want to map the unique name of each dog to the age of the dog:
const dogs = new Map([['fluffy', 10], ['barkie', 13]]); dogs.forEach((value, key) => console.log(key, value));
How is Map
different from an Object
?
An Object
is also a collection of key value pairs and can fulfill often the same purpose as a Map
can (which is creating key-value pairs). However, there are some key differences between a Map
and an Object
:
Map
is built in Iterable, this allows it to use the for of
loop or its implementation of the forEach()
method which an plain JS Object
cannot use.Map
has some nice built in methods on its prototype
which makes working with it very nicely. Because al Objects
inherit from Object.prototype
is has access to more useful methods. For example, the size()
method on Map
returns the number of keys in the Map
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With