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: ???
JavaScript 2015 (ES6) introduced a feature called Map. Not to be confused with the . map() array method, the built-in Map object is another way to structure your data. Maps are collections of distinct and ordered key-value pairs.
The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array. Values created with anonymous functions are methods of your object. Simple values are properties.
What is a Dictionary? A dictionary can also be called a map in JavaScript, and maps/dictionaries are used to store unique elements of key-value pairs. They are similar to the set data structure only that the set data structure stores a unique element of value value pairs.
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
No, ES6 does not have a literal notation for Map
s or Set
s.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With