In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what map means in es6. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
In es6, map is a data structure, a collection of "key-value", and key can be any type of data; map provides a "value-to-value" correspondence, is a more complete implementation of hash structure, and the syntax is "new Map ([iterable])".
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What is the meaning of map in es6
What is Map?
Previously, there was no such data collection as Map in ES5, and ES6 added it.
Map is a collection of key-value, and 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 structures. It is similar to objects and is also a collection of key-value pairs, but the scope of "keys" is not limited to strings, and all types of values (including objects) can be used as keys.
In other words, the Object structure provides a "string-value" correspondence, while the Map structure provides a "value-value" correspondence, which is a more complete Hash structure implementation.
If you need a "key-value pair" data structure, Map is more appropriate than Object.
Grammar
New Map ([iterable])
An 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 is added to the 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 press a key to access a value, delete a key, and detect whether a key is bound to a value. So (and there are no other built-in alternatives) we used to use objects as Maps in the past. However, there are some important differences between Maps and Objects, and using Map is a better choice in the following situations
The key of an Object can only be a string or Symbols, but the key of a Map can be any value, including functions, objects, and primitive types.
The key values in Map are ordered, while the keys added to the object are not. Therefore, when you traverse it, the Map object returns the key values in the order in which they are inserted.
You can get the number of key-value pairs of a Map directly through the size attribute, while the number of key-value pairs of Object can only be calculated manually.
Map can iterate directly, while the iteration of Object requires getting its array of keys before iterating.
Object has its own prototype, and the key name on the prototype chain may conflict with the key name you set on the object. Although ES5 can initially use map = Object.create (null) to create an object without a prototype, this usage is not very common.
Map has some performance advantages in scenarios involving frequent addition and deletion of key-value pairs.
Examples are as follows:
/ / string, as key, and JS object are similar to var map = new Map () / / setmap.set ('name',' John') / / two parameters, which are divided into key,value in the corresponding map, and automatically check the type when you push it. Object,String,Array and other lmap.set ('age', 29) / / getmap.get (' name') / / Johnmap.get ('age') / / 29 do this to the code. It doesn't look as simple as JS objects, but the power of Map is that its key can be any type of / / object as a key presentation var xy = {x: 10, y: 20} / / coordinates var wh = {w: 100,200} / / width and height var map = new Map () / / setmap.set (xy, 'coordinates') map.set (wh) 'width and height') / / getmap.get (xy) / / 'coordinates' map.get (wh) / / 'width and height'
Results:
This is the end of the article on "what does map in es6 mean?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.