Java教程

javascript中的数组、Map、Set和Iterator

本文主要是介绍javascript中的数组、Map、Set和Iterator,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

javascript中的数组、Map、Set和Iterator

数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        "use strict"
        var age = [1,2,3,4,56,5,12,45];
        age.forEach(function (value){
            console.log(value);
        })

        console.log("=======================");

        // var var index in object
        for(var num in age){
            if(age.hasOwnProperty(num)){
                console.log("存在");
            }
            console.log(age[num]);
        }
    </script>
</head>
<body>

</body>
</html>

Map

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        "use strict"

        //Es6新特性 Map和Set
        //学生的名字 成绩
        var map = new Map([['zhangsan',22],['lisi',80],['wangwu',70],['zjaoliu',40]]);
        var name = map.get("zhangsan");
        console.log(name);
        map.set('admin',75);//新增
        console.log(map.get("admin"));
        map.delete("zhangsan");//删除
        console.log(map);

    </script>
</head>
<body>

</body>
</html>

Set

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        "use strict"
        var set = new Set([3,34,22,22,22]);
        console.log(set);
        set.add(23);
        console.log(set);
        set.add(34);//添加
        console.log(set);
        set.delete(3);//删除
        console.log(set);
        console.log(set.has(34));//是否包含某个元素
    </script>
</head>
<body>

</body>
</html>

Iterator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        "use strict"

        //es6新特性
        var a = [1,2,3,4];

        for (let x of a){
             console.log(x);
        }

        console.log("=========================");

        var map = new Map([['zhangsan',1],['lisi',2],['wangwu',3]]);
        for (let x of map){
            console.log(x);
        }

        console.log("=========================");

        var set = new Set([1,2,3]);
        for (let x of set){
            console.log(x);
        }

    </script>
</head>
<body>

</body>
</html>
这篇关于javascript中的数组、Map、Set和Iterator的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!