JavaScript can get the map value through subscript. The Map object saves key/value pairs and is a collection of key/value pairs. Any value (object or primitive value) can be used as a key or a value. The Object structure provides "string-value" correspondence, and the Map structure provides "value-value" correspondence.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Map object saves key/value pairs and is a collection of key/value pairs. Any value (object or primitive) can be used as a key or a value. The Object structure provides "string-value" correspondence, and the Map structure provides "value-value" correspondence.
JavaScript to get map value example:
map对象如下: var mapObject = { id1001: '小明', id1002: '小花' }
Get the value corresponding to id 'id001':
for(var key in mapObject){ var value = mapObject[ key ]; //注意是 [ ] alert(value); //依次弹出小明、小花 }
Comparison of Object and Map object:
Similar Point: Both allow you to access a value by pressing a key, delete a key, and detect whether a key is bound to a value.
Differences:
1. 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 basic types.
2. 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.
3. The number of key-value pairs of a Map can be obtained directly through the size attribute, while the number of key-value pairs of Object can only be calculated manually.
4. Map is iterable, and iteration of Object requires first obtaining its key array and then iterating.
5. Object has its own prototype, so the key names on the prototype chain may conflict with the key names on the object. Although ES5 can use map = Object.create(null) to create an object without a prototype, this usage is less common.
6. Map will have some performance advantages in scenarios where key-value pairs are frequently added or deleted.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to read the value of map in javascript. For more information, please follow other related articles on the PHP Chinese website!