作者:qinyu1982_469 | 来源:互联网 | 2023-10-12 12:01
//创建map
const map = new Map([
['a',1],
['b',2]
]);
console.log(map) //{"a" => 1, "b" => 2}
//map属性
console.log(map.size) //2
//map类的方法
map.set('matthew','huo').set('new','fq')
console.log(map) //{"a" => 1, "b" => 2, "matthew" => "huo", "new" => "fq"}
//get 获取对应的key值
console.log(map.get('new')) //fq
console.log(map.get('abs')) //undefined
//delete(key) 删除 如果有 返回true 否则false
console.log(map.delete('a')) //true
console.log(map.delete('a')) //false
//has(key) 返回布尔值 表示某个键值存在
console.log(map.has('b')) //true
console.log(map.has('a')) // false
//keys 返回所有键名
console.log(map.keys()) //{"matthew", "new"}
//values 返回所有键值
console.log(map.values())
//entries 打印所有键值对
console.log(map.entries());
//forEach 遍历每个成员
map.forEach(function(key,value,map){
console.log(key + ':' + value)
})
//注意事项
console.log(map.set({},'x').set({},'y')) //{"b" => 2, "matthew" => "huo", "new" => "fq", Object {} => "x", Object {} => "y"}
//clear 清空所有数据
map.clear();
console.log(map)