当前位置:网站首页>Difference between map and object

Difference between map and object

2022-06-24 20:29:00 Gentle breeze - grass

Map Follow object The difference between

MapObject
Unexpected key value Map By default, there are no keys Object There's a prototype , The key name on the prototype chain may conflict with the setting key name of your own object
Type of key Map The bond of can be any value , Include functions , Object or any basic type Object Must be String perhaps Symbol
The order of the keys Map Of Key Is ordered . So in iterations Map In the order in which the objects are inserted Object Is chaotic
SizeMap The key value logarithm of can be easily passed through Size Property acquisition Object The number of key value pairs of can only be calculated manually
iteration Map yes iterable Of , So it can be iterated directly iteration Object You need to get its key in some way before you can iterate
performance It performs better in the scenario of frequent addition and deletion of key value pairs In the scenario of frequent addition and deletion of key value pairs, no optimization is made

 Insert picture description here

Map The data structure has the following operation methods :

  • size: map.size return Map The total number of members of the structure .
  • set(key,value): Set the key name key Corresponding key value value, Then go back to the whole Map structure , If key There has been a value , Then the key value will be updated , Otherwise, the new key will be generated .( Because it returns the current Map object , So you can chain call )
  • get(key): This method reads key Corresponding key value , If you can't find it key, return undefined.
  • has(key): This method returns a Boolean value , Indicates whether a key is currently Map In the object .
  • delete(key): This method deletes a key , return true, If the deletion fails , return false.
  • clear():map.clear() Clear all members , no return value .

Map Structure native provides three ergodic generator functions and one ergodic method

  • keys(): Returns the traverser of the key name .
  • values(): The traverser that returns the key value .
  • entries(): Returns the traverser of all members .
  • forEach(): Traverse Map All members of .
原网站

版权声明
本文为[Gentle breeze - grass]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241358467766.html

随机推荐