Home >Web Front-end >Front-end Q&A >What are the traversal methods of es6 map objects?

What are the traversal methods of es6 map objects?

青灯夜游
青灯夜游Original
2022-04-11 17:02:096782browse

The map traversal methods are: 1. Use the forEach() function, the syntax "map.forEach(function(value,key){...}"; 2. Use the "for..of" loop statement, Syntax "for([key, value] of map object){...}".

What are the traversal methods of es6 map objects?

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

Map is a structure of key-value pairs, with extremely fast search speed. It is created by passing in an array of arrays.

##Map object The traversal method

    ##forEach()
  • ##for..of traverses keys, values, entries
  • iterator.next() traverses keys, values, entries
  • ##1. forEach() function
var map = [{  
             key : "百度",  
             value : "李彦宏"  },
            {
             key : "阿里巴巴",  
             value : "马云"  },
        ];  
map.forEach(function(value, key) {
  console.log(key, value);
})

2. for..of loopWhat are the traversal methods of es6 map objects?

keys = map.keys();
for (key of keys) {
  console.log(key);  // map.get(key)可得value值。
}
values = map.values();
for (value of values) {
  console.log(value);
}
entries = map.entries();
for ([key, value] of entries) {
  console.log(key, value);
}

##[Related recommendations:

javascript video tutorial

What are the traversal methods of es6 map objects?webfrontend

The above is the detailed content of What are the traversal methods of es6 map objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn