s 6——新規データタイプオブジェクト集合Map
37566 ワード
Map
特徴:は対象と類似しており、本質的にはキーパッドペアのセット である.「キー」は文字列に限定されず、各種の値(オブジェクトを含む)はキー として使用されてもよい.オブジェクト「文字列−値」、map「値−値」は、より完全なhash構造の実現 である.キーは唯一で、同じキーの後は前の をカバーします.
size
1.setとget Map回転配列 配列はMap を回転します. Map変換オブジェクト オブジェクト変換Map 受け入れる対象はキー名として、他のタイプの値をキー名として受け入れません. 弱い引用、キーの指す対象はごみ回収メカニズムに算入しないで、メモリが漏れて を防止します.は遍歴できません.size属性もなく、クリアできません.
特徴:
{
let obj = {a: 1}, obj1 = {b: 2}, obj2 = {};
obj2.c = 3;
//
obj2['d'] = 4;
obj2[obj] = 5;
console.log(obj.toString());//[object Object]
console.log(obj, obj1, obj2);//{a: 1} {b: 2} {c: 3, d: 4, [object Object]: 5}
}
map {
let obj = {a: 1}, obj1 = {b: 2};
// map
const map = new Map([
['name', 'bob'],
['name', 'lucy'],//
['age', 18],
[obj, 'good'],
[obj1, 'bad'],
[[1, 2], 'hhh']
]);
console.log(map);//Map(5) {"name" => "lucy", "age" => 18, {…} => "good", {…} => "bad", Array(2) => "hhh"}
}
属性size
{
let obj = {a: 1}, obj1 = {b: 2};
const map = new Map([
['name', 'bob'],
['name', 'lucy'],
['age', 18],
[obj, 'good'],
[obj1, 'bad'],
[[1, 2], 'hhh']
]);
console.log(map.size);//5 ( )
}
方法1.setとget
{
let obj = {a: 1}, obj1 = {b: 2},obj2=[1,2];
const map = new Map([
[obj, 'good'],
[obj1, 'bad'],
[obj2, 'hhh']
]);
//set( )
console.log(map.set('friends', ['tom', 'john']).set('num', 20));//Map(5) {{…} => "good", {…} => "bad", Array(2) => "hhh", "friends" => Array(2), "num" => 20}
console.log(map);//Map(5) {{…} => "good", {…} => "bad", Array(2) => "hhh", "friends" => Array(2), "num" => 20}
//get( )
console.log(map.get('num'));//20
console.log(map.get(obj));//good
console.log(map.get(obj2));//hhh
console.log(map.get('friends'));//(2) ["tom", "john"]
}
2.delete(チェーン編集には対応していません) {
let obj = {a: 1}, obj1 = {b: 2};
// map
const map = new Map([
['name', 'bob'],
['name', 'lucy'],//
['age', 18],
[obj, 'good'],
[obj1, 'bad'],
[[1, 2], 'hhh']
]);
console.log(map.delete('name'));//true
console.log(map.delete('xxx'));//false
console.log(map);//Map(4) {"age" => 18, {…} => "good", {…} => "bad", Array(2) => "hhh"}
}
3.has {
const map = new Map([
['name', 'bob'],
['age', 18],
]);
console.log(map.has('name'));//true
console.log(map.has('xxx'));//false
}
4.clear {
const map = new Map([
['name', 'bob'],
['age', 18],
]);
console.log(map.clear());//undefined
console.log(map);//Map(0) {}
}
5.keys values entries {
const map = new Map([
['name', 'bob'],
['age', 18],
]);
console.log(map.keys());//MapIterator {"name", "age"}
console.log(map.values());//MapIterator {"bob", 18}
console.log(map.entries());//MapIterator {"name" => "bob", "age" => 18}
}
巡回する {
const map = new Map([
['name', 'bob'],
['age', 18],
]);
map.forEach(function(value,index){
console.log(index);//name; age
console.log(value);//bob; 18
})
}
注意事項 {
const map = new Map([
['name', 'bob'],
['age', 18],
]);
map.set({},'hhhh');
map.set({},'aaaa');
//
console.log(map);//Map(4) {"name" => "bob", "age" => 18, {…} => "hhhh", {…} => "aaaa"}
console.log({}==={});//false
}
他のデータ構造との相互変換 {
const myMap = new Map().set(true, 7).set({ foo: 3 }, ['abc'])
console.log([...myMap])// (2) [Array(2), Array(2)]
}
{
const myMap = new Map([
[true, 7],
[{ foo: 3 }, ['abc']]
])
console.log(myMap)// Map(2) {true => 7, {…} => Array(1)}
}
{
function strMapToObj (strMap) {
let obj = Object.create(null)
for (let [k, v] of strMap) {
obj[k] = v
}
return obj
}
const myMap = new Map().set('yes', true).set('no', false)
console.log(strMapToObj(myMap))// {yes: true, no: false}
}
{
function objToStrMap (obj) {
let strMap = new Map()
for (let k of Object.keys(obj)) {
strMap.set(k, obj[k])
}
return strMap
}
console.log(objToStrMap({ yes: true, no: false }))// Map(2) {"yes" => true, "no" => false}
}
WeakMapconst wm = new WeakMap()
const dom = document.getElementById('id')
wm.set(dom, 'some information')
wm.get(dom)