4 ms·
That looks like a cumbersome way to define a map, like small arrays within arrays.
by varikin 12y ago
That looks like a cumbersome way to define a map, like small arrays within arrays.
- raverbashing 12y agoHummm My bet is that the Map() is turning an array of arrays into a map. And that in the future it will convert the original syntax into a Map as well
- shawnz 12y agoThe syntax they have there is creating an array of arrays, and then passing that to the Map constructor, like you suspect. But it wouldn't be possible to make that syntax produce a Map directly -- if it did, how would you produce an array of arrays? EDIT: My mistake, I misunderstood what you were saying.
- nawitus 12y agoIt's easy to do with a helper function. For example, this works: var objectToMap = x => new Map(Object.keys(x).map(o => [o, x[o]]));
- raverbashing 12y agoBy original syntax I meant Map({a:1, b:2}) Yes, I wouldn't want to change the syntax for array of arrays.
- masklinn 12y agoIt's an array of (key, value) pairs. The Map constructor takes an iterable of pairs.
- underwater 12y agoIt's probably to get around the non-deterministic ordering of objects. New Map({'a': 'bar', '1': 'foo'}); Would flip the order of the keys.
- bryanlarsen 12y agoTo be pedantic, in all modern browsers the order is deterministic. But it certainly can be surprising. For those who aren't aware, browser Javascript maintains insertion order, except for numeric keys, which are in numeric order ahead of all other keys.
- johnhenry 12y agoDo you have a source on this by chance?