Bootstrap

javascript Map常用操作

const map = new Map();
map.set('dog','狗');
map.set('cat','猫');
console.log(map.size);

const keys = [...map.keys()]
console.log(keys);

const values = [...map.values()]
console.log(values);

for(let [key,value] of map.entries()){
    console.log(key);
    console.log(value);
}

console.log(map.has('dog'));
console.log(map.get('dog'))
map.delete('cat')
console.log(map.size);

map.forEach((key, value) => {
    console.log(key);
    console.log(value);
})

map.clear()
console.log(map.size);
2
[ 'dog', 'cat' ]
[ '狗', '猫' ]
dog
狗
cat
猫
true
狗
1
狗
dog
0

ts版

let mapDic : Map<string,string> = new Map()
mapDic.set('dog','狗');
mapDic.set('cat','猫');
console.log(mapDic.size);

const mapkeys: string[] = [...mapDic.keys()]
console.log(mapkeys);

const mapvalues: string[] = [...mapDic.values()]
console.log(mapvalues);

for(let [key,value] of mapDic.entries()){
    console.log(key);
    console.log(value);
}

console.log(mapDic.has('dog'));
console.log(mapDic.get('dog'))
mapDic.delete('cat')
console.log(mapDic.size);

mapDic.forEach((key: string, value: string) => {
    console.log(key);
    console.log(value);
})

mapDic.clear()
console.log(mapDic.size);

;