JavaScript: Iterate Over Map Object

By Xah Lee. Date: . Last updated: .

Iterate Over Map with for-of Loop

use for-of Loop

for (let [k, v] of mapObj) {body}
iterate over Map mapObj's items. k is key, v is value.
let xx = new Map([["a", 1], ["b", 2], ["c", 3]]);

// iterate over map with key and value
for (let [k, v] of xx) {
  console.log(k, v);
}

// a 1
// b 2
// c 3
for (let x of mapObj) {body}
iterate over Map mapObj's items. Each key/val pair is assigned to x as array [key, val].
let xx = new Map([["a", 1], ["b", 2], ["c", 3]]);

// iterate over map items
for (let xpair of xx) {
  console.log(xpair, Array.isArray(xpair));
  // each item is array
}

// [ 'a', 1 ] true
// [ 'b', 2 ] true
// [ 'c', 3 ] true

Iterate Over Map with forEach Method

Map.prototype.forEach

JavaScript Map Object

BUY Ξ£JS JavaScript in Depth