What is the equivalent of
var object = {
'foo': 'bar',
1: 42
}
using an ES6 Map?
The closest you can get is:
let object = new Map([
['foo', 'bar'],
['1', 42]
]);
Important things to notice:
In modern browsers it can be as simple as:
new Map(Object.entries(object))
This NPM package called map-literal does exactly what you want.
Object.entries()
and new Map()
can make a Map
object for you, but it cannot deal with nested structures. Therefore you will need to use a package for that.