JavaScript: Iterate Over Map Object

By Xah Lee. Date: . Last updated: .

Iterate Over Map with for-of Loop

for (let [k, v] of map_obj) {…}
iterate over Map map_obj's elements. k is key, v is value. [see for-of Loop]
for (let x of map_obj) {…}
iterate over Map map_obj's elements. Each key/val pair is assigned to x as array [key,val].
let myMap = new Map([["a", 1], ["b", 2], ["c", 3]]);

// iterate over map with key and value
for (let [k, v] of myMap) {
  console.log(k, v);
}
// prints
// a 1
// b 2
// c 3
let myMap = new Map([["a", 1], ["b", 2], ["c", 3]]);

// iterate over map elements
for (let pair of myMap) {
    console.log(pair, Array.isArray ( pair )); // each element is array
}

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

Iterate Over Map with forEach Method

Map.prototype.forEach

JavaScript Map Object

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc