Thursday, April 12, 2018

How to use object as Keys in Map

We can also use object as key in map.

Note: ES6 map is different. Do not make confuse yourself.

Example I:


let testObject = { name: "TestObject With Map" };

let testMap = new Map();

testMap.set(testObject, 123);

alert( testMap.get(testObject) ); // 123

Here testObject is the simple normal object which I used as key in map.

How Map comparing keys ?

Map uses the algorithm SameValueZero (same as strict equality === ).

We can also do the chaining operation with map. See below example,



testMap.set('1', 'stringOne')
       .set(1, 'number1')
       .set(true, 'booleanTrue');


Like this you can add n number of times.







No comments:

Post a Comment