Blog A Paranoid Guy

JavaScript数组相关方法

2016-05-15

检测数组

  • 单框架网页

即只有一个全局执行环境,直接用 instanceof 操作符即可:

    var arr1 = new Array();
    console.log(arr1 instanceof Array); //true
  • 多框架网页

有多个全局执行环境,ES5 新增了 Array.isArray() 方法检测,推荐这样做

    var arr2 = new Array();
    console.log(Array.isArray(arr2)); //true

转换方法

  • toString()

将数组中的每个以字符串的方式拼接成新的字符串,默认以逗号分隔

  • valueOf()

返回数组本身

  • toLocaleString()

返回值和 toString() 一样,不过内部调用的是每个数组项的 toLocaleString 方法

  • join()

该方法接受一个参数,即用作分隔的字符串,然后返回所有数组项的字符串

    var colors = ['red', 'blue', 'green'];
    console.log(colors.toString()); //red,blue,green
    console.log(colors.valueOf()); //[red, blue, green]
    console.log(colors.toLocaleString()); //red,blue,green
    console.log(colors.join('||'));

栈方法和队列方法

  • push()

该方法接受任意数量的参数,并逐个添加到数组末尾,然后返回修改后数组的长度

  • pop()

该方法从数组末尾移除最后一项,然后返回移除的项

  • shift()

该方法从数组前端移除第一项,然后返回移除的项

  • unshift()

该方法接受任意数量的参数,并逐个添加到数组前端,然后返回修改后数组的长度

    var colors = ['red', 'blue', 'green'];
    console.log(colors.push('yellow', 'black')); //5
    console.log(colors.pop()); //black
    console.log(colors.shift()); //red
    console.log(colors.unshift('white', 'orange')); //5
    console.log(colors); //["white", "orange", "blue", "green", "yellow"]

重排序方法

  • reverse()

反转数组项的顺序:

    var num = [1, 5, 2, 4, 3];
    console.log(num.reverse()); //[3, 4, 2, 5, 1]
  • sort()

该方法会调用每个数组项的 toString() 方法,比较得到的字符串,按照升序排列; 但它可以接受一个比较函数作为参数,用于比较:

    var num = [1, 5, 2, 4, 3];
    function compare(value1, value2) {
        return value1 - value2; //升序排列
    }
    console.log(num.sort(compare)); // [1, 2, 3, 4, 5]

操作方法

  • concat()

基于当前数组的所有项创建一个新的数组,并不改变原来的数组

    var colors = ['red', 'green', 'blue'];
    console.log(colors.concat('yellow', 'black')); //["red", "green", "blue", "yellow", "black"]
    console.log(colors); //["red", "green", "blue"]
  • slice()

基于当前数组中的一个或多个项创建一个新的数组。slice() 方法接受一或多个参数,即要返回项的起始位置和结束位置,但不包括结束位置的项

    var colors = ['red', 'green', 'blue', 'yellow', 'black'];
    console.log(colors.slice(1)); //["green", "blue", "yellow", "black"]
    console.log(colors.slice(1,3)); //["green", "blue"]
  • splice()

该方法接受三个参数:起始位置,要删除的项数,和要插入的项

    var colors = ['red', 'green', 'blue'];
    colors.splice(0,2); //删除前两项
    console.log(colors); //['blue']
    colors.splice(0, 0 , 'red', 'green'); //插入两项
    console.log(colors); //["red", "green", "blue"]
    colors.splice(1, 1, 'yellow'); //替换第二项
    console.log(colors); //["red", "yellow", "blue"]

位置方法

  • indexOf()

接受两个参数:要查找的项和表示查找起点位置的索引,没找到返回-1

    var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    console.log(num.indexOf(3)); //2
    console.log(num.lastIndexOf(3)); //6
  • lastIndexOf()

indexOf() 方法相反,从后往前找


迭代方法

ES5 为数组定义了5个迭代方法。每个方法都接受两个参数:要在每一项运行的函数和(可选的)运行该函数的作用域对象。传入函数的参数包括三个参数: 数组项的值,该项在数组中的位置和数组对象本身

  • every() 和 some()

every() 来说,传入函数的每一项返回 true ,这个方法才返回 true ; 否则,它就返回 falsesome()every() 方法相反,传入函数有一项返回 true ,就会返回 true 。简单来说,every() 就是与逻辑,some() 就是或逻辑

    var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];

    var everyResult = num.every(function (item, index, num){
        return (item > 2);
    });
    console.log(everyResult); //false

    var someResult = num.some(function (item, index, num){
        return (item > 2);
    });
    console.log(someResult); //true
  • filter()

对数组每一项运行给定函数,返回 true 的项组成的数组

    var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];

    var filterResult = num.filter(function (item, index, num){
        return (item > 2);
    });
    console.log(filterResult); //[3, 4, 5, 4, 3]
  • map()

对数组每一项运行给定函数,返回每次函数调用结果组成的数组

    var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];

    var mapResult = num.map(function (item, index, num){
        return item * 2;
    });
    console.log(mapResult); //[2, 4, 6, 8, 10, 8, 6, 4, 2]
  • forEach()

该方法没有返回值,执行某些操作

    num.forEach(function (item, index, num){
        // 执行操作
    });

归并方法

  • reduce() 和 reduceRight()

这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。reduce() 从前往后,reduceRight() 从后往前。接受两个参数:一个函数和(可选的)作为归并的初始值。 函数参数接受四个值:前一个值、当前值、项的索引和数组对象

    var num = [1, 2, 3, 4, 5];
    var sum = num.reduce(function (prev, cur, index, array) {
        return prev + cur;
    });
    console.log(sum); //15 

最后附思维导图一张:


参考资料

JavaScript高级程序设计(第3版)


Comments