数组的操作

Posted lovemis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组的操作相关的知识,希望对你有一定的参考价值。

  • 创建数组
  • <script>
    var i;
    var mycars = new Array();
    mycars[0] = "Saab";
    mycars[1] = "Volvo";
    mycars[2] = "BMW";
    for (i=0;i<mycars.length;i++){
        document.write(mycars[i] + "<br />");
    }
    </script>
  • 合并两个数组 - concat()
  • <script>
    var hege = ["Cecilie", "Lone"];
    var stale = ["Emil", "Tobias", "Linus"];
    var children = hege.concat(stale);
    document.write(children);
    </script>
  • 合并三个数组 - concat()
  • <script>
    var parents = ["Jani", "Tove"];
    var brothers = ["Stale", "Kai Jim", "Borge"];
    var children = ["Cecilie", "Lone"];
    var family = parents.concat(brothers, children);
    document.write(family);
    </script>
  • 用数组的元素组成字符串 - join()
  • <script>
    function myFunction(){
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        var x=document.getElementById("demo");
        x.innerhtml=fruits.join();
    }
    </script>
  • 删除数组的最后一个元素 - pop()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function myFunction(){
        fruits.pop();
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>
  • 数组的末尾添加新的元素 - push()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function myFunction(){
        fruits.push("Kiwi")
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>
  • 反转一个数组中的元素的顺序 - reverse()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function myFunction(){
        fruits.reverse();
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>
  • 删除数组的第一个元素 - shift()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function myFunction(){
        var delell = fruits.shift();
        var x=document.getElementById("demo");
        x.innerHTML= ‘删除后数组为:‘ +  fruits;
        document.getElementById("demo2").innerHTML= ‘删除的元素是:‘ +  delell;
    }
    </script>
  • 从一个数组中的选择元素 - slice()
  • <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>
  • 数组排序(按字母顺序升序)- sort()
  • <script>
    function myFunction(){
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.sort();
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>
  • 数字排序(按数字顺序升序)- sort()
  • <script>
    function myFunction(){
        var points = [40,100,1,5,25,10];
        points.sort(function(a,b){return a-b});
        var x=document.getElementById("demo");
        x.innerHTML=points;
    }
    </script>
  • 数字排序(按数字顺序降序)- sort()
  • <script>
    function myFunction(){
        var points = [40,100,1,5,25,10];
        points.sort(function(a,b){return b-a});
        var x=document.getElementById("demo");
        x.innerHTML=points;
    }
    </script>
  • 在数组的第2位置添加一个元素 - splice()
  • <script>
    function myFunction(){
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.splice(2,0,"Lemon","Kiwi");
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>
  • 转换数组到字符串 -toString()
  • <script>
    function myFunction(){
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        var str = fruits.toString();
        var x=document.getElementById("demo");
        x.innerHTML= str;
    }
    </script>
  • 在数组的开头添加新元素 - unshift()
  • <script>
    function myFunction(){
        var fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.unshift("Lemon","Pineapple");
        var x=document.getElementById("demo");
        x.innerHTML=fruits;
    }
    </script>








































































































以上是关于数组的操作的主要内容,如果未能解决你的问题,请参考以下文章

C 语言字符串操作 ( 使用 数组下标 操作字符串 | 使用 char * 指针 操作字符串 )

Kotlin入门到精通(实战)数组的创建和操作详解

Kotlin入门到精通(实战)数组的创建和操作详解

JS 数值操作 一维数组转二维数组快捷操作

MongoDB 实用数组聚合操作 (2)

# 数组及操作