关于数组方法的总结

首先,我们来说不改变原数组的

1.concat  返回连接后的新数组

let arr = [1,2,3,4,5];
let arr2 = [6,7,8,9,10];
console.log(arr.concat(arr2));//打印1-10    

2.slice(n,m) 

从下标为n的开始截取,一直到m,但不包括有m
var
arr = [1,3,5,7,9,11]; var arrCopy = arr.slice(1); var arrCopy2 = arr.slice(1,4); var arrCopy3 = arr.slice(1,-2); var arrCopy4 = arr.slice(-4,-1); console.log(arr); //[1, 3, 5, 7, 9, 11](原数组没变) console.log(arrCopy); //[3, 5, 7, 9, 11] console.log(arrCopy2); //[3, 5, 7] console.log(arrCopy3); //[3, 5, 7] console.log(arrCopy4); //[5, 7, 9]

3.join(separator)

将数组中的元素组起一个字符串,以separator为分割符

var arr = [1,2,3];
console.log(arr.join()); // 1,2,3
console.log(arr.join("-")); // 1-2-3
console.log(arr); // [1, 2, 3]

4.toString()

改变原数组的

5.push()

在末尾处添加参数,返回修改后数组长度

var arr = ["Lily","lucy","Tom"];
var count = arr.push("Jack","Sean");
console.log(count); // 5
console.log(arr); // ["Lily", "lucy", "Tom", "Jack", "Sean"]

6.pop

数组末尾移除最后一项,返回移除的项

let arr = [1,2,3,4,5];
let item = arr.pop()
console.log(arr);//1,2,3,4
console.log(item);  //5 

7.shift和unshift

        let item = arr.shift();
        console.log(item);//1
        console.log(arr);//2,3,4,5
        let item2 = arr.unshift(5);
        console.log(item2);//5
        console.log(arr);//5,2,3,4,5    

8.reverse

反转数组项的顺序

var arr = [13, 24, 51, 3];
console.log(arr.reverse()); //[3, 51, 24, 13]
console.log(arr); //[3, 51, 24, 13]

9.sort

 在排序时,他会调用toString方法,比较得到的字符串,用来确定如何排序,即使数组中的每一项都是数值,它比较的也是字符串。比较的顺序优先级,数字>英文>中文

var arr1 = ["a", "d", "c", "b"];
console.log(arr1.sort()); // ["a", "b", "c", "d"]
arr2 = [13, 24, 51, 3];
console.log(arr2.sort()); // [13, 24, 3, 51]
console.log(arr2); // [13, 24, 3, 51]

为了解决上述问题,可以接收一个比较函数作为参数,用来决定是升序还是降序

升序排列

function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
arr = [1,2, 3, 4,5];
console.log(arr.sort(compare)); //1,2,3,4,5

降序排列

mpare(value1, value2) {
if (value1 < value2) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0;
}
}
let arr = [1,2,3,4,5];
console.log(arr.sort(compare)); //5,4,3,2,1

10.splice

 可以实现删除,插入和替换

删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。例如, splice(0,2)会删除数组中的前两项。

插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。例如,splice(2,0,4,6)会从当前数组的位置 2 开始插入4和6。
替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。例如,splice (2,1,4,6)会删除当前数组位置 2 的项,然后再从位置 2 开始插入4和6。

splice()方法始终都会返回一个数组,该数组中包含从原始数组中删除的项,如果没有删除任何项,则返回一个空数组

var arr = [1,3,5,7,9,11];
var arrRemoved = arr.splice(0,2);
console.log(arr); //[5, 7, 9, 11]
console.log(arrRemoved); //[1, 3]
var arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr); // [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2); // []
var arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr); // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3); //[7]

 11.indexOf()和lastIndexOf()

indexOf(n,m)  查找n从下标为m的位置开始,从前往后查找,返回查找项在数组中的位置,没有找到返回-1

lastIndexOf(n,m)  查找n从下标为m的位置开始,从后往前查找,返回查找项在数组中的位置,没有找到返回-1

var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5)); //2
console.log(arr.lastIndexOf(5)); //5
console.log(arr.indexOf(5,2)); //2
console.log(arr.lastIndexOf(5,4)); //2
console.log(arr.indexOf("5")); //-1

12.forEach()

对数组进行遍历循环,没有返回值,参数是function;类型,参数分别是:遍历的数组内容,对应的数组索引,数组本身

var arr = [1, 2, 3, 4, 5];
arr.forEach(function(x, index, a){
console.log(x + '|' + index + '|' + (a === arr));
});
// 输出为:
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true

13.map()

生成给定的函数调用的结果组成的数组

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.map(function(item){
return item*item;
});
console.log(arr2); //[1, 4, 9, 16, 25]

14.filter()

过滤,生成满足条件的数组

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr2 = arr.filter(function(x) {
return x>8
})
console.log(arr2); //[9, 10]

15.every()

判断数组中每一项是否都满足条件,只有都满足条件,才会返回true

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.every(function(x) {
return x < 10;
}); 
console.log(arr2); //true
var arr3 = arr.every(function(x) {
return x < 3;
}); 
console.log(arr3); // false

16.some()

判断数组中是否有满足条件的项,有一项满足条件,就返回true

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.some(function(x) {
return x < 3;
}); 
console.log(arr2); //true
var arr3 = arr.some(function(x) {
return x < 1;
}); 
console.log(arr3); // false

17.reduce()和reduceRight()

reduce()方法接收一个函数callbackfn作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值。

  • preValue: 上一次调用回调返回的值,或者是提供的初始值(initialValue)
  • curValue: 数组中当前被处理的数组项
  • index: 当前数组项在数组中的索引值
  • array: 调用 reduce()方法的数组

利用reduce实现数组求和

var arr = [1,2,3,4,5,6];
 
Array.prototype.sum = function (){
    var sumResult = 0;
    return this.reduce(function (preValue, curValue) {
        return sumResult = preValue + curValue;
    });
    return sumResult;
}
arr.sum(); // 21
var arr = [1,2,3,4,5,6];
 
console.time("ruduce");
Array.prototype.ruduceSum = function (){
    for (var i = 0; i < 10000; i++) {
        return  this.reduce (function (preValue, curValue) {
            return preValue + curValue;
        });
    }
}
arr.ruduceSum();
console.log('最终的值:' + arr.ruduceSum()); // 21
console.timeEnd("ruduce"); // 0.417ms

reduceRight()方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加。

对一个数组求和,也可以使用reduceRight()方法:

var arr = [1,2,3,4,5,6];
 
console.time("ruduceRight");
Array.prototype.ruduceRightSum = function (){
    for (var i = 0; i < 10000; i++) {
        return  this.reduceRight (function (preValue, curValue) {
            return preValue + curValue;
        });
    }
}
arr.ruduceRightSum();
console.log('最终的值:' + arr.ruduceSum()); // 21
console.timeEnd("ruduceRight"); // 5.725ms
原文地址:https://www.cnblogs.com/zoutuan/p/12027421.html