How to initialize a Map in ES6/ES2015 similar to an Object expression?
Solution 1:
The closest you can get is:
let object = new Map([
['foo', 'bar'],
['1', 42]
]);
Important things to notice:
- Object properties are identified by strings, while Map keys can be any value, so make sure all keys are strings in the input array.
- Iterating a Map object yields entries by insertion order. That is not guaranteed for objects, so behavior might be different.
Solution 2:
In modern browsers it can be as simple as:
new Map(Object.entries(object))