Home >Web Front-end >Front-end Q&A >What does map in es6 mean?
In es6, map is a data structure, a collection of "key-value". Key can be any type of data; map provides a "value-to-value" correspondence, which is a more complete Hash structure implementation, the syntax is "new Map([iterable])".
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
What is Map
Before ES5, there was no such data collection as Map. Only in ES6 did it Added in.
Map is a collection of key-values. The key can be any type of data, similar to an object, but the key of an object can only be a string.
ES6 provides Map data structure. It is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings. Various types of values (including objects) can be used as keys.
In other words, the Object structure provides "string-value" correspondence, and the Map structure provides "value-value" correspondence, which is a more complete implementation of the Hash structure.
If you need a "key-value pair" data structure, Map is more suitable than Object.
Syntax
new Map([iterable])
Iterable can be an array or other iterable object whose elements are key-value pairs (an array of two elements, for example: [[ 1, ' one' ],[ 2, 'two' ]]).
Each key-value pair will be added to a new Map.
null will be treated as undefined.
Comparison of Object and Map:
Objects and Maps are similar in that they both allow you to access a value by pressing a key, delete a key, and detect whether a key is bound. value. Therefore (and there is no built-in alternative) we have always treated objects as Maps. However, there are some important differences between Maps and Objects. Using Map would be a better choice in the following situations
The keys of an Object can only be strings or Symbols, but the keys of a Map Keys can be any values, including functions, objects, and primitive types.
The key values in the Map are ordered, but the keys added to the object are not. Therefore, when iterating over it, the Map object returns key values in the order of insertion.
You can directly obtain the number of key-value pairs of a Map through the size attribute, while the number of key-value pairs of Object can only be calculated manually.
Map can be iterated directly, while iteration of Object requires first obtaining its key array and then iterating.
Object has its own prototype, and the key names on the prototype chain may conflict with the key names you set on the object. Although ES5 can use map = Object.create(null) to create an object without a prototype, this usage is less common.
Map will have some performance advantages in scenarios involving frequent additions and deletions of key-value pairs.
Examples are as follows:
// 字符串作为key,和JS对象类似 var map = new Map() // set map.set('name', 'John')//两个参数,分为对应map中key,value, 推进去的时候会自动检查类型,Object,String,Array等l map.set('age', 29) // get map.get('name') // John map.get('age') // 29 这么对代码,看起来确实没有JS对象简洁 但Map的强大之处在于它的key可以是任意类型 // 对象作为key演示 var xy = {x: 10, y: 20} // 坐标 var wh = {w: 100, h: 200} // 宽高 var map = new Map() // set map.set(xy, '坐标') map.set(wh, '宽高') // get map.get(xy) // '坐标' map.get(wh) // '宽高'
Result:
[Related recommendations:javascript video tutorial、web front-end】
The above is the detailed content of What does map in es6 mean?. For more information, please follow other related articles on the PHP Chinese website!