美文网首页
2018-09-17

2018-09-17

作者: 网网会想念 | 来源:发表于2018-09-17 17:02 被阅读0次

    数组API的用法和意思

    1.toString()将数组转为字符串并返回。

          <p id="demo">点击按钮将数组转为字符串并返回。</p>
            <button onclick="myFunction()">点我</button>
            <script>
                function myFunction(){
                    var fruits = ["Banana", "Orange", "Apple", "Mango"];
                    var str = fruits.toString();
                    var x=document.getElementById("demo");
                    x.innerHTML= str;
                }
    

    2.slice()截取数组下标 1 到 2 的元素

    <p id="demo">点击按钮截取数组下标 1 到 2 的元素。</p>
            <button onclick="myFunction()">点我</button>
            <script>
                function myFunction(){
                    var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
                    var citrus = fruits.slice(1,3);
                    var x=document.getElementById("demo");
                    x.innerHTML=citrus;
                }
            </script>
    

    3.unshift()在数组中插入元素

     <p id="demo">单击按钮在数组中插入元素。</p>
                <button onclick="myFunction()">点我</button>
                <script>
                    function myFunction(){
                        var fruits = ["Banana", "Orange", "Apple", "Mango"];
                        fruits.unshift("Lemon","Pineapple");
                        var x=document.getElementById("demo");
                        x.innerHTML=fruits;
                    }
                </script>
    

    4.splice()向数组添加元素

    <p id="demo">点击按钮向数组添加元素。</p>
            <button onclick="myFunction()">点我</button>
            <script>
                function myFunction(){
                    var fruits = ["Banana", "Orange", "Apple", "Mango"];
                    fruits.splice(2,0,"Lemon","Kiwi");
                    var x=document.getElementById("demo");
                    x.innerHTML=fruits;
                }
            </script>
    

    5.reverse()将数组反转排序

    <p id="demo">单击按钮将数组反转排序。</p>
            <button onclick="myFunction()">点我</button>
            <script>
                var fruits = ["Banana", "Orange", "Apple", "Mango"];
                function myFunction(){
                    fruits.reverse();
                    var x=document.getElementById("demo");
                    x.innerHTML=fruits;
                }
            </script>
    

    6.push()给数组添加新的元素

    <p id="demo">单击按钮给数组添加新的元素。</p>
            <button onclick="myFunction()">点我</button>
            <script>
                var fruits = ["Banana", "Orange", "Apple", "Mango"];
                function myFunction(){
                    fruits.push("Kiwi")
                    var x=document.getElementById("demo");
                    x.innerHTML=fruits;
                }
            </script>
    

    7.pop()移除最后一个数组元素

    p id="demo">单击按钮删除数组的最后一个元素。</p>
        <button onclick="myFunction()">点我</button>
        <script>
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            function myFunction(){
                fruits.pop();
                var x=document.getElementById("demo");
                x.innerHTML=fruits;
            }
        </script>
    

    8.join('')把数组中的所有元素转换为一个字符串

    <p id="demo">点击按钮将数组作为字符串输出。</p>
         <button onclick="myFunction()">点我</button>
        <script>
            function myFunction(){
                var fruits = ["Banana", "Orange", "Apple", "Mango"];
                var x=document.getElementById("demo");
                x.innerHTML=fruits.join(" and ");
            }
        </script>
    

    9.concat()把两个数组拼接起来

       var hege = ["Cecilie", "Lone"];
        var stale = ["Emil", "Tobias", "Linus"];
        var children = hege.concat(stale);
        document.write(children);
    

    10.shife的用法.移除一个东西

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.shift('Banana')
        document.write(fruits)
    

    相关文章

      网友评论

          本文标题:2018-09-17

          本文链接:https://www.haomeiwen.com/subject/jhsrnftx.html