Javascript Map [@@iterator]()

Introduction

The Javascript Map @@iterator returns the iterator for the Map object.

The map iterator function returns the same as the entries() function by default.

myMap[Symbol.iterator]
const myMap = new Map();//from   ww  w  .  j  a va  2s.c  om
myMap.set('0', 'foo');
myMap.set('key', 'value');
myMap.set('lang', 'CSS');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

const mapIter = myMap[Symbol.iterator]();

console.log(mapIter.next().value);
console.log(mapIter.next().value);
console.log(mapIter.next().value);

Using [@@iterator]() with for..of

const myMap = new Map()/*from   w ww.  j  a  v a  2 s  .c om*/
myMap.set('key', 'value');
myMap.set('lang', 'CSS');
myMap.set('0', 'foo')
myMap.set(1, 'bar')
myMap.set({}, 'baz')

for (const entry of myMap) {
  console.log(entry);
}

for (const [key, value] of myMap) {
  console.log(`${key}: ${value}`);
}



PreviousNext

Related