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 does map in es6 mean?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

In ES6, you can use the reverse() method of the array object to achieve array reversal. This method is used to reverse the order of the elements in the array, putting the last element first and the first element last. The syntax "array.reverse()". The reverse() method will modify the original array. If you do not want to modify it, you need to use it with the expansion operator "...", and the syntax is "[...array].reverse()".

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() to find the difference set, with the syntax " new Set([...newA].filter(x =>!newB.has(x)))", the difference set elements will be included in a set collection and returned; 3. Use Array.from to convert the set into an array Type, syntax "Array.from(collection)".

For browser compatibility. As a new specification for JS, ES6 adds a lot of new syntax and API. However, modern browsers do not have high support for the new features of ES6, so ES6 code needs to be converted to ES5 code. In the WeChat web developer tools, babel is used by default to convert the developer's ES6 syntax code into ES5 code that is well supported by all three terminals, helping developers solve development problems caused by different environments; only in the project Just configure and check the "ES6 to ES5" option.

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

No, require is the modular syntax of the CommonJS specification; and the modular syntax of the es6 specification is import. require is loaded at runtime, and import is loaded at compile time; require can be written anywhere in the code, import can only be written at the top of the file and cannot be used in conditional statements or function scopes; module attributes are introduced only when require is run. Therefore, the performance is relatively low. The properties of the module introduced during import compilation have slightly higher performance.

The map is ordered. The map type in ES6 is an ordered list that stores many key-value pairs. The key names and corresponding values support all data types; the equivalence of key names is determined by calling the "Objext.is()" method. Implemented, so the number 5 and the string "5" will be judged as two types, and can appear in the program as two independent keys.
