Js数组的基本方法1

来源:互联网 发布:广东中标数据科技 编辑:程序博客网 时间:2024/05/20 10:56

    本文主要介绍的方法主要有以下几个,这几个方法都会改变当前操作的数组内容。

  • Array.prototype.pop()
  • Array.prototype.push()
  • Array.prototype.shift()
  • Array.prototype.unshift()
  • Array.prototype.reverse()
  • Array.prototype.sort()
  • Array.prototype.splice()

1、Array.prototype.pop()

    pop()方法用于删除数组的最后一个元素,并返回被删除的最后一个元素。

    语法:array.pop()

    返回值:array的最后一个元素

例子:

    var arr = [1,2,3,4];    var temp = arr.pop();    console.log(temp);        //print: 4    console.log(arr);         //print: [1,2,3]    //对于一个空数组,pop元素会返回undefined    console.log([].pop());    //print: undefined
2、Array.prototype.push()
     push()方法可向数组的末尾添加一个元素或多个元素,并返回新的长度。

     语法:array.push(element1, element2,…… elementX)

     参数:element1

     描述:必需,要添加到数组的第一个元素。

      参数:element2

     描述:可选,要添加到数组的第二个元素。

     参数:elementX

     描述:可选,可添加多个元素。

     返回值:把指定的值添加到数组后的新长度。

例子:

    var color = ['red', 'green'];    var subColor = color.push('yellow', 'black');    console.log(color);      //print: ['red', 'green', 'yellow', 'black']    console.log(subColor);       //print: 4
3、Array.prototype.shift()

    shift()方法用于把数组的第一个元素从原数组删除,并返回第一个元素的值。

    语法:array.shift()

    返回值:数组原来的第一个元素的值

    说明:如果数组是空的,那么shift()方法将不进行任何操作,返回underfined值。该方法不创建新数组,而是直接修改原有的array。
例子:

    var color = ['red', 'black', 'blue', 'green'];    console.log('The color before: ' + color);   //print: The color:[ 'red', 'black', 'blue', 'green'    var shifted = color.shift();    console.log('The color after: ' + color);   //print: The color after:['black', 'blue', 'green']    console.log('Removed color: ' + shifted);  //print: Removed color:red    //对于一个空数组,shift会返回undefined    console.log([].shift());  
4、Array.prototype.unshift()

       unshift()方法将一个或者多个元素插入到数组的第一个位置,并且返回新数组的长度。[在原来的数组上操作,并且有返回值]

     语法:array.unshift(element1, element2,…… elementX)

     参数:element1

     描述:必需,要添加到数组的第一个元素。

     参数:element2

     描述:可选,要添加到数组的第二个元素。

     参数:elementX

     描述:可选,可添加多个元素。

     返回值:把指定的值添加到数组后的新长度。

例子:

    var color = ['red', 'blue'];    console.log(color.unshift('yellow'));   // the new array length is 3; color is ['yellow','red', 'blue']    console.log(color);    console.log(color.unshift('black', 'brown'));      // the new array length is 5; color is ['black', 'brown','yellow','red', 'blue']    console.log(color);    console.log(color.unshift(['pink']));        // the new array length is 6; color is ["pink","black","brown","yellow","red","blue"']    console.log(color);

5、Array.prototype.reverse()

     reverse()方法将数组中所有元素的位置进行反转。[在原来的数组上操作,并且有返回值]

    语法:array.reverse()

例子:

    var color = ['red', 'black', 'green'];    color.reverse();    console.log(color);      //print: ['green', 'black', 'red']
6、Array.prototype.sort()

     该方法对数组中所有的元素都进行排序,如果没有提供比较函数compareFunction,则按照字符串的Unicode码的顺序进行排序。

     语法:array.sort([compareFunction])

     如果为sort()参数提供了一个函数,那么该函数必须返回下列值之一:

  • 负值:如果所传递的第一个参数比第二个参数小
  • 零:如果所传递的两个参数相等
  • 正值:如果所传递的第一个参数比第二个参数大

例子:

    //比较函数没有提供的情况下:    var fruit = ['cherries', 'apples', 'bananas'];    console.log(fruit.sort());     //['apples', 'bananas', 'cherries'];    var num = [1, 10, 2, 21];    console.log(num.sort());    //[1, 10, 2, 21]    //没有提供比较函数,都是转化成字符串,然后根据字符串的Unicode码进行排序
补充compareFunction(a, b)比较函数:

  • 注意:a,b分别代表数组中比较的两个元素,可以是数值、对象、数组等
  • 如果compareFunction(a, b)返回的数值小于0,则a排在b的前面
  • 如果compareFunction(a, b)返回的数值为0,则不对a和b进行交换位置。需要注意的是ESMAscript并没有对此行为做出保证,不同的浏览器可能会有不同的做法。
  •  如果compareFunction(a, b)返回的数值大于0,则a排在b的后面
例子:
//按数值大小排序var numbers = [4, 2, 5, 1, 3];numbers.sort(function(a, b){    return a - b;});console.log(numbers);         //print: [1, 2, 3, 4, 5]//对于对象也可以按照其中的属性值进行排序var items = [    { name: 'Edward', value: 21 },    { name: 'Sharpe', value: 37 },    { name: 'And', value: 45 },    { name: 'The', value: -12 },    { name: 'Magnetic' },    { name: 'Zeros', value: 37 }];items.sort(function(a, b){    if(a.value > b.value){        return 1;    }    if(a.value < b.value){        return -1;    }    return 0;});console.log(items);//-12, 21, 37, 45, undefined, 37; 因为itesms[4]中的对象没有value属性,无法进行比较,所以前面的4个是按顺序排好,最后两个还是原来的顺序。
7、Array.prototype.splice()
     splice()方法可以对数组中已经存在元素进行删除,也可以添加元素到数组中。
     
    语法:array.splice(startIndex, deleteCount[, element1, element2, ..., elementN])
  • startIndex: 改变数组的起始位置,如果startIndex大于数组的长度,则将数组的长度赋值给startIndex;如果startIndex为负数,则加上数组的长度。
  • deleteCount: 从startIndex开始,要删除元素的个数;如果deleteCount为0,则不删除数组中的元素,但是此时需要提供至少一个新的元素(即第三个参数);如果deleteCount的数值大于startIndex后面元素的个数,则将从startIndex处开始,将数组后面的元素全部删除。
  • elementN: 要添加到数组中的元素,如果该参数为空则splice()值做删除操作。
     返回值:返回值是被删除的元素,如果没有删除操作则返回空数组。
例子:
    var color = ['red', 'yellow', 'blue', 'black'];    var removedColor1 = color.splice(2, 0, 'green');    console.log(removedColor1);// removed is [], no elements removed    console.log(color);  //print:["red", "yellow", "green", "blue", "black"]    var removedColor2 = color.splice(3, 1);    console.log(removedColor2);// removed is ["blue"]    console.log(color);  //print:["red", "yellow", "green", "black"]    var removedColor3 = color.splice(2, 1, 'pink');    console.log(removedColor3);// removed is ["green"]    console.log(color);  //print:["red", "yellow", "pink", "black"]    var removedColor4 = color.splice(0, 2, 'brown','purple');    console.log(removedColor4);// removed is ["red", "yellow"]    console.log(color);  //print:["brown", "purple", "pink", "black"]

1 0