83

As of ES6, JavaScript has a proper Map object. I don't see a way to use a literal notation though, as you could with an Array or an Object. Am I missing it, or does it not exist?

Array: var arr = ["Foo", "Bar"];

Object: var obj = { foo: "Foo", bar: "Bar" };

Map: ???

Matt
  • 2,651
  • 1
  • 21
  • 28

4 Answers4

84

No, ES6 does not have a literal notation for Maps or Sets.

You will have to use their constructors, passing an iterable (typically an array literal):

var map = new Map([["foo", "Foo"], ["bar", "Bar"], …]);

var set = new Set(["Foo", "Bar", …]);

There are some proposals to add new literal syntax to the language, but none made it into ES6 (and I'm personally not confident they will make it into any future version).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Worked great! However, to all the careless readers like me: Make sure to encapsulate all of the key-value arrays into an array. I skipped right over that extra set of brackets and got a nasty TypeError. – Mr. Foots Jul 12 '16 at 00:17
20

There is a way to do it, like we do with literals

const fruits = new Map(Object.entries({apples: 1, bananas: 2}))

Alex Shwarc
  • 822
  • 10
  • 20
  • Nice, unless you have to support old browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries (well, there are polyfills, I suppose, but it reduces the interest of the trick). – PhiLho May 24 '18 at 08:10
  • 2
    Most of the places that have Map available, probably have Object.entries available. The biggest caveat here is that it only initializes a map where all the keys are strings. It could be used like `new Map([[12, 10], ...Object.entries({apples: 1, bananas: 2})])` to initialize a set with some string keys and some keys of another type, though. – Benjamin Atkin May 29 '18 at 21:22
  • 3
    Also, the act of using an object makes the value of `Map`'s insertion-order guarantee moot. At this point, just use an object. – Jason Jun 06 '18 at 02:54
  • 1
    @JasonTFeatheringham Newer versions of Ecmascript require that the insertion-order is preserved for object-literals (2015+) and `Object.entries` (2020+): https://stackoverflow.com/a/23202095/2441655 – Venryx Dec 10 '21 at 19:10
10

It's like a HashMap -- there is no literal version. You have to define it like you would a constructor.

You can read this topic which discusses map literals though, and why they should be added. It's basically others potential proposals on Map literals. I personally can't forsee a literal syntax in ES7, since Maps are very easy to use as is -- but in future proposals there could be syntactic sugar applied.

An example of potential literal notation was discussed using an octothorp (a hash) to look something like:

const myMap = Map#{expression("derp"): value("herp")};
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
1

It's 2023 and map literal syntax is still not included in ECMAScript standard, though this NPM package called map-literal does exactly what you want.

Object.entries() 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