54

What is the equivalent of

var object = {
  'foo': 'bar',
  1: 42
}

using an ES6 Map?

Amit
  • 45,440
  • 9
  • 78
  • 110
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 1
    See also [Does JavaScript have a Map literal notation?](http://stackoverflow.com/q/35654495/1048572) – Bergi Apr 15 '16 at 10:35

3 Answers3

86

The closest you can get is:

let object = new Map([
  ['foo', 'bar'],
  ['1', 42]
]);

Important things to notice:

  1. Object properties are identified by strings, while Map keys can be any value, so make sure all keys are strings in the input array.
  2. Iterating a Map object yields entries by insertion order. That is not guaranteed for objects, so behavior might be different.
Amit
  • 45,440
  • 9
  • 78
  • 110
  • Surely `['1', 42]` should be `[1, 42]` or did you make it a string because that's what it would be were it still an Object? – CodingIntrigue Oct 01 '15 at 07:17
  • 2
    Point 1 doesn't make sense. If the keys can be any value, then why should I make sure they are strings? – a better oliver Oct 03 '15 at 18:09
  • 5
    @zeroflagL - because the question is how to initialize a Map similar to an object, and object keys are always strings. If you want an equivalent, but as a Map, you need to use strings as well. – Amit Oct 03 '15 at 20:14
  • I had similar issue but as it could not work properly on every browser. Please check compatibility table about that feature "Constructor argument: new Map(iterable)" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map – Avlin Mar 21 '16 at 11:11
  • 2
    @Avlin - I wouldn't recommend using `Map` in a client side script anyway, and if it's IE11 you're targeting then you're going to be facing a lot of missing features. Instead use a polyfill or a transpiler to get stable functionality. – Amit Mar 21 '16 at 19:04
  • this seems pretty dumb, why not `new Map({foo: bar})`, i don't get it – Alexander Mills Nov 18 '17 at 05:02
  • @AlexanderMills - suggestions that don't work are dumb. Have some respect – Amit Nov 18 '17 at 09:48
  • @AlexanderMills reason being an object literal's entries are not guaranteed by insertion order. In other words, you lose that aspect of maps by way of using an object. – Jason Jun 06 '18 at 02:52
  • I think in newer JS engines, the order of keys is preserved, but perhaps they have to remain compliant with older engines. – Alexander Mills Jun 06 '18 at 02:56
  • Also note that in some cases we don't care about key order, in fact, the vast majority of the time. – Alexander Mills Jun 06 '18 at 02:57
17

In modern browsers it can be as simple as:

new Map(Object.entries(object))
Dimitris
  • 599
  • 5
  • 3
  • 2
    Note that because an object's key insertion order isn't guaranteed, you will lose that assertion. `.entries` may return items in a different order. – Jason Jun 06 '18 at 02:50
0

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.

temeddix
  • 11
  • 2