欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript数组(array)的常用方法(shift/unshift/pop/push/concat/splice/reverse/sort/slice/join)

程序员文章站 2023-12-23 12:54:28
...

数组array

今天简单的介绍一下array对象的常用方法(shift/unshift/pop/push/concat/splice/reverse/sort/slice/join)在这就不多说直接撸代码


1.获取数组的长度 length

2.删除数组的第一个元素 shift

3.删除数组的最后一个元素 pop

4.删除或替换数组中的值,返回删除的值 splice 

5. 在数组前添加元素,返回数组长度 unshift

6.在数组后添加元素,返回数组长度 push

7.将数组反续 reverse

8.按指定的参数排序 sort

9.连接两个或更多的数组,并返回拼接结果(独立的新数组) concat

10.数组截取,返回一个截取结果(独立的新数组) slice

11.将数组元素组成一个字符串   join

12.将数组转化成字符串 toString 

13.将数组转换为本地数组 toLocaleString
代码示例如下:
(实例中arr数组是一直随着每条命令修改而变化的不是固定一个值的)
<script type="text/javascript">
var arr=["a","b","c","d","e","f","g","h","i","j"];
// 数组的长度 length
   var arrLength=arr.length;  // arrLength=10


//删除数组元素 shift pop  ==>  splice(替换删除)


   // shift 删除数组的第一个元素,返回删除的值
      var arrShift=arr.shift();   // arr=["b","c","d","e","f","g","h","i","j"]
                                  // arrShift="a"

   // pop 删除数组的最后一个元素,返回删除的值
      var arrPop=arr.pop();  // arr=["b","c","d","e","f","g","h","i"]
                             // arrPop="j"

   // splice 删除或替换数组中的值,返回删除的值  splice(开始值下标,个数,替换的值,替换的值.....)
      var arrSplice=arr.splice(2,2)   // arr=["b","c","f","g","h","i"]
                                     // arrSplice=["d","e"]


      var arrSpliceTwo=arr.splice(0,3,"a","b","c")   // arr=["a","b","c","g","h","i"]
                                                            // arrSpliceTwo=["b","c","f"]


// 添加数组元素 unshift push 

   // 在数组前添加元素,返回数组长度
      var arrUnshift=arr.unshift("1");   // arr=["1","a","b","c","g","h","i"]
                                         // arrUnshift=7


   // 在数组后添加元素,返回数组长度
      var arrPush=arr.push("j");   // arr=["1","a","b","c","g","h","i","j"]
                                   // arrPush=8

// 给数组内部元素排序 reverse sort


   // 将数组反续 reverse
      var arrReverse=arr.reverse();   // arr=["j","i","h","g","c","b","a","1"]
                                      // arrReverse=["j","i","h","g","c","b","a","1"]


   // 按指定的参数排序 sort 可以传递函数进行计算
      var arrSort=arr.sort();   // arr=["1","a","b","c","g","h","i","j"]
                                // arrSort=["1","a","b","c","g","h","i","j"]


//连接两个或更多的数组,并返回结果 concat
   var arrOne=[2,3,4];
   var arrTwo=[6,7,8];
   var arrS=["a","b","c"];
   var arrArr=arrOne.concat(arrTwo);   //arrArr=[2, 3, 4, 6, 7, 8]
   var arrArrT=arrOne.concat(arrTwo,arrS);   //arrArrT=[2, 3, 4, 6, 7, 8, "a", "b", "c"]
   var arrArrS=arrOne.concat("5","6","7");   //arrArrS=[2, 3, 4, "5", "6", "7"]
   //当concat()的传值为空时,则返回一个和原数组相同的独立的新数组,此时新数组和原数组没有任何关联引用关系
   //我们可以用来数组复制
   var arrClone=arrS.concat();//此时arrClone为一个新数组无论arrS元素如何变动arrClone都不会变


// 数组截取 slice slice(start,end) 不包括end值
   var arrSlice=arr.slice(2,5);   // arr=["1","a","b","c","g","h","i","j"]
                                  // arrSlice= ["b", "c", "g"]
    //当slice()的传值为空时,则返回一个和原数组相同的独立的新数组,此时新数组和原数组没有任何关联引用关系
var arrClone1=arr.slice();//此时arrClone1为一个新数组无论arr元素如何变动arrClone1都不会变
// 数组变为字符串 join toString toLocaleString 
    // 将数组元素组成一个字符串  以()中的值为分割符,默认为","     
    arr=["1","a","b","c","g","h","i","j"];   
    var arrJoin=arr.join(""); // arrJoin= 1abcghij    
    var arrJoinTwo=arr.join(); // arrJoinTwo=1,a,b,c,g,h,i,j     
    var arrJoinS=arr.join("-"); // arrJoinS=1-a-b-c-g-h-i-j//将数组转化成字符串    
    var arrString=arr.toString(); // arrString= 1,a,b,c,g,h,i,j
   //将数组转换为本地数组   
    var arrLocaleString=arr.toLocaleString(); // arrString= 1,a,b,c,g,h,i,j
</script>

相关标签: Array javascript

上一篇:

下一篇: