Map
Objects in JavaScript (Object), is essentially a collection of key-value pairs, but only to do with a string key name. This gives its use is a big limitation.
To solve this problem, ES6 provides a Map data structure. It is similar to the object, but also a set of key-value pairs, but the scope of the "key" is not limited to character string, various kinds of values (including objects) can be used as keys.
In other words, Object structures provide - corresponds to the "string value", Map provides the structure - corresponds to the "value" of a better realization of JSON data structures.
If you need a more "relaxed" "key on the" data structure, Map more appropriate than Object.
Features: key-value pair => any type => better deal with a problem mapping needs.
Create a Map
Map itself is a constructor, when using the constructor, by passing the data initialization parameter.
let m = new Map();
Map function may be acceptable an array (or array-like objects) as a parameter to be initialized. But the difference is with the Set, Map of members in the array is an array of key-value pairs expressed a pair.
let m = new Map([["name", "zhangsan"], ["age", 20]]);
Map attributes
It is a common attribute: size returns total membership of the Map instance.
let m = new Map([["name", "zhangsan"], ["age", 20]]); console.log( m.size );//2
Map method
The method of Example Map into two categories: Usage operation (operation for data) and traversal methods (for traversing data).
Methods of operation:
set (key, value) add or modify data. Set key corresponding to the key value, and returns the structure itself Map
get (key) to obtain data. Read key corresponding to the key, if not find the key, returns undefined
has (key) to see if there is a data and returns a Boolean value.
delete (key) to delete the data. Deleted successfully return true
clear () to clear all data, no return value
Map = the let new new the Map ([[ "name", "zhangsan"], [ "Age", 20 is ]]); // set value name Lisa map.set ( "name", "Lisa" ); Console. log (Map); // the Map (2) { "name" => "Lisa", "Age" => 20 is} // Get the name corresponding to the value of the let as map.get the getMap = ( "name" ); the console.log (getMap); // Lisa // see if there are age the let hasMap = map.has ( "age" ); console.log (hasMap); // to true // delete key age for the let delMap = the Map. the delete ( " Age " ); Console.log( delMap ); // true //Clear all data map.clear (); the console.log (Map); // the Map (0) {}
Traversal methods:
Map provides three functions and traversal generates a traversal methods:
keys () Returns a key name iterator
values () Returns a key of the iterator
entries () Returns a key-value pair iterator
forEach () callback function traverse each member
let num = new Map([["one", 1], ["two", 2], ["three", 3]]); for(let key of num.keys()){ console.log(key); } // one // two // three for(let value of num.values()){ console.log(value); } // 1 // 2 // 3 for(let item of num.entries()){ console.log(item[0], item[1]); } // one 1 // two 2 // three 3 // 将上面代码通过解构优化 for(let [key, value] of num.entries()){ console.log(key, value); } // one 1 // two 2 // three 3 num.forEach((value, key) => { console.log(value, key) }) // 1 one // 2 two // 3 three
Interchangeable with other data structures
Map into an array
Map into an array of the most convenient method is to use the extended operator ....
= myMap the let new new the Map (); myMap the .set ( to true , "true" ) the .set ( false , "false"); // because each will return a new Map, can be attached to write console.log (myMap); / / {to true => "true", false => "false"} the let newMap = [... the myMap]; the console.log (newMap); // [[to true, "true"], [false, "false" ]]
Array into Map
Map of the array passed in the constructor, you can turn Map.
let arr = [[true, "真"], [false, "假"]]; let map = new Map(arr); console.log(map); // {true => "真", false => "假"}
Map into objects
If the Map all the keys are strings, it can be converted to objects.
function strMapToObj(strMap){ let obj = {}; for(let [k, v] of strMap){ obj[k] = v; } return obj; } let myMap = new Map().set("green","绿").set("red","红"); console.log(myMap); // {"green" => "绿", "red" => "红"} console.log( strMapToObj(myMap) ); // { green: "绿", red: "红" }
Objects into Map
function objToStrMap(obj){ let strMap = new Map(); for(let item in obj){ strMap.set( item, obj[item] ) } return strMap; } let obj = { name: "zhangsan", age: 20 }; console.log( objToStrMap(obj) ); // {"name" => "zhangsan", "age" => 20}