引言💭
JavaScript 中的数组是非常常用的数据结构,它提供了丰富的方法来操作数组元素、进行遍历、修改,下面就来介绍几种常用的方法。
1. push() 和 pop()
1.1 push()
push() 方法用于向数组的末尾添加一个或多个元素,并返回新的数组长度。
let arr = [1, 2, 3];
let newLength = arr.push(4, 5);
console.log(arr); // 输出: [1, 2, 3, 4, 5]
console.log(newLength); // 输出: 5
1.2 pop()
pop() 方法用于删除数组的最后一个元素,并返回该元素的值。它会改变原数组的长度。
let arr = [1, 2, 3, 4];
let removedElement = arr.pop();
console.log(arr); // 输出: [1, 2, 3]
console.log(removedElement); // 输出: 4
2. shift() 和 unshift()
2.1 shift()
shift() 方法用于删除数组的第一个元素,并返回该元素的值。它会改变原数组的长度。
let arr = [1, 2, 3];
let removedElement = arr.shift();
console.log(arr); // 输出: [2, 3]
console.log(removedElement); // 输出: 1
2.2 unshift()
unshift() 方法用于在数组的开头添加一个或多个元素,并返回新的数组长度。
let arr = [2, 3];
let newLength = arr.unshift(1);
console.log(arr); // 输出: [1, 2, 3]
console.log(newLength); // 输出: 3
3. concat()
concat() 方法用于合并两个或多个数组,并返回一个新的数组,原数组不变。
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // 输出: [1, 2, 3, 4]
console.log(arr1); // 输出: [1, 2] // 原数组不变
4. slice()
slice() 方法返回数组的一个片段(浅拷贝),不修改原数组。它接受两个参数,start(起始索引)和 end(结束索引,结束位置不包含)。
let arr = [1, 2, 3, 4, 5];
let sliced = arr.slice(1, 4);
console.log(sliced); // 输出: [2, 3, 4]
console.log(arr); // 输出: [1, 2, 3, 4, 5] // 原数组不变
5. splice()
splice() 方法用于添加或删除数组中的元素。它可以用来从数组中删除元素、向数组中插入元素,或替换元素。
- 删除元素:
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 2); // 从索引2开始,删除2个元素
console.log(arr); // 输出: [1, 2, 5]
- 插入元素:
let arr = [1, 2, 5];
arr.splice(2, 0, 3, 4); // 在索引2的位置插入元素3和4
console.log(arr); // 输出: [1, 2, 3, 4, 5]
- 替换元素:
let arr = [1, 2, 3, 4];
arr.splice(1, 2, 'a', 'b'); // 从索引1开始,删除2个元素,插入'a'和'b'
console.log(arr); // 输出: [1, 'a', 'b', 4]
6. forEach()
forEach() 方法用于对数组中的每一项执行指定的回调函数。它不会改变原数组,且返回值是 undefined。
let arr = [1, 2, 3];
arr.forEach((element, index) => {
console.log(index, element); // 输出:0 1, 1 2, 2 3
});
7. map()
map() 方法创建一个新数组,数组中的每个元素是调用回调函数返回的结果。它不会修改原数组。
let arr = [1, 2, 3];
let doubled = arr.map(x => x * 2);
console.log(doubled); // 输出: [2, 4, 6]
console.log(arr); // 输出: [1, 2, 3] // 原数组不变
8. filter()
filter() 方法用于创建一个新数组,包含所有通过测试的元素。回调函数必须返回 true 或 false,返回 true 的元素会被包含在新数组中。
let arr = [1, 2, 3, 4, 5];
let evenNumbers = arr.filter(x => x % 2 === 0);
console.log(evenNumbers); // 输出: [2, 4]
9. reduce() 和 reduceRight()
9.1 reduce()
reduce() 方法对数组中的每一项执行一个“累加器”函数(从左到右),将其汇总为单一的结果值。
let arr = [1, 2, 3, 4];
let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 输出: 10
9.2 reduceRight()
reduceRight() 方法与 reduce() 相似,但它是从右到左(即倒序)遍历数组。
let arr = [1, 2, 3, 4];
let sum = arr.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 输出: 10
10. find() 和 findIndex()
10.1 find()
find() 方法返回数组中第一个满足测试条件的元素。若没有找到符合条件的元素,则返回 undefined。
let arr = [1, 2, 3, 4, 5];
let firstEven = arr.find(x => x % 2 === 0);
console.log(firstEven); // 输出: 2
10.2 findIndex()
findIndex() 方法返回数组中第一个满足测试条件的元素的索引。若没有找到符合条件的元素,则返回 -1。
let arr = [1, 2, 3, 4, 5];
let evenIndex = arr.findIndex(x => x % 2 === 0);
console.log(evenIndex); // 输出: 1
11. some() 和 every()
11.1 some()
some() 方法用于检查数组中是否有至少一个元素满足给定的条件。只要有一个元素符合条件,就返回 true,否则返回 false。
let arr = [1, 2, 3, 4];
let hasEven = arr.some(x => x % 2 === 0);
console.log(hasEven); // 输出: true
11.2 every()
every() 方法用于检查数组中是否所有元素都满足给定的条件。只有所有元素都符合条件时,才返回 true,否则返回 false。
let arr = [2, 4, 6, 8];
let allEven = arr.every(x => x % 2 === 0);
console.log(allEven); // 输出: true
12. sort()
sort() 方法用于对数组中的元素进行排序。默认情况下,它会根据字符编码点排序元素,但对于数字数组,通常需要传递一个比较函数来指定排序规则。
let arr = [4, 1, 3, 2];
arr.sort();
console.log(arr); // 输出: [1, 2, 3, 4]
let numbers = [10, 20, 5, 15];
numbers.sort((a, b) => a - b);
console.log(numbers); // 输出: [5, 10, 15, 20]
结语✒️
欢迎补充,持续更新中……🚀🚀🚀