zoukankan      html  css  js  c++  java
  • javascript 数组方法总结

    1.push():在数组尾部添加一个或多个元素,返回数组新的长度

     arrayObject.push(newelement1,newelement2,....,newelementX)

     newelement1:必需要添加的参数。要添加到数组的第一个元素,剩下的参数可选。 

    var a=['hello','world'];
    var length= a.push(2,[4,3]);
     console.log(a); //[ 'hello', 'world', 2, [ 4, 3 ] ]
     console.log(a.length);//4;
     console.log(length);//4;返回值
    
     length=a.push(4);
     console.log(a); //[ 'hello', 'world', 2, [ 4, 3 ], 4 ]
     console.log(a.length); //5
     console.log(length);//5;返回值
    示例

     2.pop():删除数组最后一个元素,返回删除的值,如果数组已经为空,则 pop() 不改变数组,并返回 undefined 值。 

     var a=['hello','world',1,2,'hi'];
      var arg=a.pop();
     console.log(arg);//hi;返回值为删除的元素,删除数组最后一个元素
     console.log(a);//[ 'hello', 'world', 1, 2 ]
     
     //当数组为空
     var arr=[];
     console.log(arr.pop());//undefined
     console.log(arr);//[]
    示例

    3.shift():删除数组第一个元素,返回删除的值,如果数组是空的,那么 shift() 方法将不进行任何操作,返回 undefined 值 

    var a=['hello','world',1,2,'hi'];
    
     var arg=a.shift();
     console.log(arg);//hello;返回值为删除的元素,删除数组的第一个元素
     console.log(a);//[ 'world', 1, 2, 'hi' ]
    
     //当数组为空
     var arr=[];
     console.log(arr.shift());//undefined
     console.log(arr);//[]
    示例

    4. unshift():在数组头部添加一个或多个元素,返回数组新的长度

     arrayObject.unshift(newelement1,newelement2,....,newelementX)

     newelement1:必需添加的参数,向数组添加的第一个元素,剩余参数可选

    var a=['hello','world'];
     var newlength=a.unshift('how',[8,9],'are');
     console.log(newlength);//5;返回值为新数组的长度
     console.log(a);//[ 'how', [ 8, 9 ], 'are', 'hello', 'world' ]
    示例

    5.splice(index,howmany,element1,... ...,elementN):从指定位置删除指定数量元素并增加新的元素,先执行删除操作,删除指定个数的元素,然后再插入元素或数组,splice是直接对原数组进行操作,返回值是被删除的元素组成的数组.

       index:指定位置删除或插入

       howmany:删除多少元素

       elements:插入元素

    var a=['hello','world','how','are','you'];
    
     var arg= a.splice(2,1,'?','what');
     console.log(arg);//[ 'how' ];返回值删除的元素
     console.log(a);//[ 'hello', 'world', '?', 'what', 'are', 'you' ]
    示例

    6.concat():把数组原来的元素和新的元素连接起来存放在创建的新数组里,原数组保持不变,返回创建的新数组

      arrayObject.concat(arrayX,arrayX,......,arrayX)

      arrayX:必需参数,该参数可以是具体的值,也可以是数组对象。可以是任意多个

    var a=['hello','world'];
    
     var arg= a.concat('how','are','you');
     console.log(arg);//[ 'hello', 'world', 'how', 'are', 'you' ];返回值为连接后的新数组
     console.log(a);//[ 'hello', 'world' ]
    示例

    7.slice(start, [end]) ):返回指定数组的一段

     start:必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。

     end:可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。

    var a=['hello','world','how','are','you'];
    
     var arg= a.slice(3);
     console.log(arg);//[ 'are', 'you' ];返回值为从3开始到数组末尾的数组片段
     console.log(a);//[ 'hello', 'world', 'how', 'are', 'you' ]
     console.log(a.slice(3,2));//[]
     console.log(a.slice(3,4));//[ 'are' ]
     console.log(a.slice(3,-1));//[ 'are' ]
     console.log(a.slice(3,-2));//[]
     console.log(a.slice(0,-3));//[ 'hello', 'world' ]
     console.log(a.slice(-5,-3));//[ 'hello', 'world' ]
    示例

    8. join():将数组的所有元素,用选定的分隔符,转化为字符串并连接在一起,返回最后生成的字符串,不指定分隔符默认用逗号(,)   

    //数组有多个元素
     var a=['hello','world',1,2];
     console.log(a.join());//hello,world,1,2
     console.log(a.join(""));//helloworld12
     console.log(a.join(" "));//hello world 1 2
     console.log(a.join ("="));//hello=world=1=2
    //数组有一个元素
     var a=['hello'];
     console.log(a.join());//hello
     console.log(a.join(""));//hello
     console.log(a.join(" "));//hello
     console.log(a.join ("="));//hello
    示例

    9. sort():返回排序后数组。没有参数,默认按照字母排序

    arrayObject.sort(sortby)

    var a=['e','a','d','c','b'];
     console.log(a.sort());//[ 'a', 'b', 'c', 'd', 'e' ],返回值为排序后的数组,没有参数默认为按照字母排序
     console.log(a);//[ 'a', 'b', 'c', 'd', 'e' ]
     var b=[1,300,20,250,100];
     console.log(b.sort(sort));//[ 1, 20, 100, 250, 300 ]从小到大
     var b1=[1,300,20,250,100];
     console.log(b1.sort(sortReverse));//[ 300, 250, 100, 20, 1 ],从大到小
     function sort(a,b){
         return a-b;
     }
     function sortReverse(a,b){
         return b-a;
     }
    示例

    10.reverse() :方法用于颠倒数组中元素的顺序。

    var a=['e','a','d','c','b'];
     console.log(a.reverse());//[ 'b', 'c', 'd', 'a', 'e' ],返回值为颠倒后的数组
     console.log(a);//[ 'b', 'c', 'd', 'a', 'e' ]
    示例

    11.toSource() :表示对象的源代码,通常由 JavaScript 在后台自动调用,并不显式地出现在代码中。

    12.toString():把数组转换为字符串,并返回结果。

    var a=['e','a','d','c','b'];
     console.log(a.toString());//e,a,d,c,b,返回值为字符串
     console.log(a);// ['e', 'a', 'd', 'c', 'b' ]
    示例

    13.toLocaleString():把数组转换为本地字符串。

    arrayObject.toLocaleString() 

    14.valueOf() :返回 Array 对象的原始值,通常由 JavaScript 在后台自动调用,并不显式地出现在代码中。

    
    
    
  • 相关阅读:
    Best Time to Buy and Sell Stock III
    Valid Palindrome
    Longest Substring Without Repeating Characters
    Copy List with Random Pointer
    Add Two Numbers
    Recover Binary Search Tree
    Anagrams
    ZigZag Conversion
    Merge k Sorted Lists
    Distinct Subsequences
  • 原文地址:https://www.cnblogs.com/greenteaone/p/4193146.html
Copyright © 2011-2022 走看看