js中map对象获取key+value,以及常用方法
var map = new Map();//设值map.set("A","aaaa");map.set("B","bbbb");map.set("C","cccc");map.set("D","dddd");console.log(map)//结果// 0: {"A" => "aaaa"}// 1: {"B" => "bbbb"}// 2: {"C" => "...
·
var map = new Map();
//设值
map.set("A","aaaa");
map.set("B","bbbb");
map.set("C","cccc");
map.set("D","dddd");
console.log(map)
//结果
// 0: {"A" => "aaaa"}
// 1: {"B" => "bbbb"}
// 2: {"C" => "cccc"}
// 3: {"D" => "dddd"}
//取值
let v =map.get("A")
console.log(v)
//结果:aaaa
//根据Key修改Value
map.set("B","XXX")
console.log(map)
//结果
// 0: {"A" => "aaaa"}
// 1: {"B" => "XXX"}
// 2: {"C" => "cccc"}
// 3: {"D" => "dddd"}
//判断key是否存在
let boo = map.has("D");
console.log(boo)
//结果 true
//删除
map.delete("C");
//结果
// 0: {"A" => "aaaa"}
// 1: {"B" => "XXX"}
// 3: {"D" => "dddd"}
//获取key,value
map.forEach(function (value, key, map) {
alert("key:"+key+"~~~"+"value:"+value)
})
//结果
//key:A~~~value:aaaa
//key:B~~~value:XXX
//key:D~~~value:dddd
更多推荐
所有评论(0)