In ECMAScript 6 Map type is an ordered list of key-value pairs, where both the key and the value can have any type. A Map object iterates its elements in insertion order.
The forEach
method executes a provided function once per each key/value pair in the Map object, in insertion order.
var a = new Map;
a.set("first", "first");
a.set("2", "2");
a.set("34", "34");
a.set("1", "1");
a.set("second", "second");
a.forEach(function(value, key) {
console.log(key + ' => ' + value)
})
You can also use for...of
loop and it returns an array of [key, value]
for each iteration.
var a = new Map;
a.set("first", "first");
a.set("2", "2");
a.set("34", "34");
a.set("1", "1");
a.set("second", "second");
for (var p of a) console.log(p)
solved JavaScript – How to create an associative array that guarantees order?