重学ES系列之新型数据结构Map应用

2022-06-16 10:27:21 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新型数据结构Map应用</title>
</head>
<body>
    
</body>
<script>
    // 1、    
    // let num = 123;
    // let arr =[1,2,3];
    // let fun = function () {};
    // let obj = {};
    // const map1 = new Map();
    // map1.set(num,"q1"),
    // map1.set(arr,"q2");
    // map1.set(fun,"q3");
    // map1.set(obj,"q4");
    // console.log(map1);
    // console.log(map1.keys());
    // for (const key of map1.keys()) {
    //     console.log(key);
    // }
    
    // 2、
    const map2 = new Map([
        ["a1","1"],
        ["a2","2"],
        ["a3","3"]
    ]);
    map2.set("a4","4");
    map2.delete("a2");
    console.log(map2.has("a2"));//false
    console.log(map2);
    let arr1 = [...map2.values()];//可以把所有的value转存到数组
    console.log(arr1);
    let arr2 = [...map2.keys()];//可以把所有的key转存到数组
    console.log(arr2);
    let arr3 = [...map2.entries()];//显示数组里面的所有内容
    console.log(arr3);
    for (const key of map2.keys()) {
        console.log(key);
    }
    // 适用于集合类型
</script>
</html>

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119836503

0 人点赞