ES6 the map data structure learning

Meet the needs of a very sick in the project, and then found ES6 the map can be resolved, it is simple to learn a bit map.

Javascript Object data structure of key-value pairs in itself, but in fact attributes and values ​​constitute a "string - value" of the property can only be a string, if passed object literal as a property name, then by default the the object into a string, the result of this property name becomes "[object Object]" :.

ES6 provide a "value - the value" data structure, the key name not only can be a string, it can also be a target. It is a better Hash structure.

1, key-value pair, the key can be an object

const map1 = new Map()
const objkey = {p1: 'v1'}
map1.set(objkey, 'hello')
console.log(map1.get(objkey))

result:

hello

 

2, Map takes an array as a parameter, or a member of an array of array, wherein there are two elements, a representative value represents a bond

const map2 = new Map([
  ['name', 'Aissen'],
  ['age', 12]
])
console.log(map2.get('name'))
console.log(map2.get('age'))

result:

Aissen
 12

 

Guess you like

Origin www.cnblogs.com/zoeeying/p/11384952.html