在JavaScript中,数组是一种常见的数据结构,而数组方法则是操作数组的重要工具。本文将详细介绍一些常用的JS数组方法,并对比新老方法的优劣势,帮助你更好地掌握数组操作的技巧。
1 push()和pop()
push()方法用于向数组末尾添加一个或多个元素,多个参数时逗号分隔
pop()方法用于删除数组的最后一个元素,并返回被删除元素,一次仅删除一个,无需传参。
const arr = [1, 2, 3];
arr.push(4); // arr -> [1, 2, 3, 4]
arr.push(5,6); // arr -> [1, 2, 3, 4, 5, 6]
const item = arr.pop(); // item -> 6
2 unshift()和shift()
shift()方法用于删除并返回数组的第一个元素,unshift()方法用于向数组开头添加一个或多个元素。
const arr = [1, 2, 3];
arr.shift(); // arr -> [2, 3]
arr.unshift(0); // arr -> [0, 2, 3]
arr.unshift(-2,-1); // arr -> [-2,-1,0, 2, 3]
3 concat()
concat()方法用于连接两个或多个数组,并返回合并后的一个新数组。
const arrA = [1, 2, 3];
const arrB = [4, 5];
const newArr = arrA.concat(arrB); // newArr -> [1, 2, 3, 4, 5]
4 slice()
slice()数组切片,用于从数组中提取指定位置的元素,截取时需指定开始下标(含)和结束下标(不含),返回由截取部分组成的新数组。
const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(1, 4); // newArr -> [2, 3, 4]
5 splice()
splice()方法用于删除、插入或替换数组的元素,并返回被删除的元素组成的新数组。
// splice(起始下标,项数,新值)
// 项数-非0为替换,0为插入
const arrA = [1, 2, 3, 4, 5];
const deletedArr = arrA.splice(1, 2); // deletedArr -> [2, 3]
const arrB = [1, 2, 3, 4, 5];
const addArr = arrB.splice(1, 0, 8); // arrB -> [1, 8, 2, 3, 4, 5]
6 map()和forEach
map()&forEach()
1、相同点
(1)都是循环遍历数组中的每一项。 (2)每次执行匿名函数都支持三个参数,参数分别为item(当前每一项),index(索引值),arr(原数组)。 (3)匿名函数中的this都是指向window。 (4)只能遍历数组。
2、不同点
(1)map()会分配内存空间存储新数组并返回,forEach()不会返回数据。 (2)forEach()允许callback更改原始数组的元素。map()返回新的数组。
const arrA = [1, 2, 3];
const newArr = arrA.map((item) => item * 2); // newArr -> [2, 4, 6]
const arrB = [1, 2, 3];
arrB.forEach((item) => {
if(item === 1) {
arrB.push(10)
}
});
// [1, 2, 3, 10]
7 filter()
filter() 方法会创建一个新的数组,其包含通过提供的函数实现的测试的所有元素。在调用 filter() 方法时,会为数组中的每个元素执行一次提供的函数,并创建一个新数组,其中包含使函数返回 true 的所有元素。
let array = [1, 2, 3, 4, 5, 6];
let newArray = array.filter(item => {
return value > 3;
});
console.log(newArray); // 输出: [4, 5, 6]
8 find()和findIndex()
find() 方法返回数组中满足提供的测试函数的第一个元素的值。如果没有找到符合条件的元素,则返回 undefined。findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。如果没有找到符合条件的元素,则返回 -1。
let arr = [5, 12, 8, 130, 44];
let newArr = arr.find(item => {
return item > 10;
});
console.log(newArr); // 输出: 12
let index = array.findIndex(item => {
return item > 10;
});
console.log(index); // 输出: 1
9 includes()
includes()方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true
10 flat()
数组的成员有时还是数组,flat()用于将嵌套的数组“拉平”,变成一维的数组。该方法返回一个新数组,对原数据没有影响。flat()默认只会“拉平”一层,如果想要“拉平”多层的嵌套数组,可以将flat()方法的参数写成一个整数,表示想要拉平的层数,默认为1。
[1, 2, [3, 4]].flat()
[1, 2, 3, 4]
11 at()
at()方法,接受一个整数作为参数,返回对应位置的成员,并支持负索引。这个方法不仅可用于数组,也可用于字符串和类型数组(TypedArray)。
const arr = [5, 12, 8, 130, 44];
arr.at(2) // 8
arr.at(-2) // 130
12 entries(),keys() 和 values()
ES6 提供三个新的方法——entries(),keys()和values()——用于遍历数组。它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
以上就是我总结的部分常用数组方法,希望对大家有帮助,有错误遗漏之处请指出,一定虚心接受~
感谢阅读,祝好~