1. push():向数组末尾添加一个或多个元素,并返回新的长度。
const arr = [1, 2, 3];
const len = arr.push(4, 5);
console.log(arr); // [1, 2, 3, 4, 5]
console.log(len); // 5
2. pop():从数组末尾删除一个元素,并返回该元素。
const arr = [1, 2, 3];
const last = arr.pop();
console.log(arr); // [1, 2]
console.log(last); // 3
3. shift():从数组开头删除一个元素,并返回该元素。
const arr = [1, 2, 3];
const first = arr.shift();
console.log(arr); // [2, 3]
console.log(first); // 1
4. unshift():向数组开头添加一个或多个元素,并返回新的长度。
const arr = [1, 2, 3];
const len = arr.unshift(0, -1);
console.log(arr); // [0, -1, 1, 2, 3]
console.log(len); // 5
5. slice():返回一个新的数组,包含从开始到结束(不包括结束)的元素。
const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(1, 4);
console.log(newArr); // [2, 3, 4]
6. splice():从数组中删除或添加元素,并返回被删除的元素。
const arr = [1, 2, 3, 4, 5];
const removed = arr.splice(1, 2, 'a', 'b');
console.log(arr); // [1, 'a', 'b', 4, 5]
console.log(removed); // [2, 3]
7. concat():返回一个新的数组,包含原数组和一个或多个数组或值。
const arr1 = [1, 2, 3];
const arr2 = [4, 5];
const newArr = arr1.concat(arr2, 6, 7);
console.log(newArr); // [1, 2, 3, 4, 5, 6, 7]
8. join():将数组中的所有元素转换为字符串,并用指定的分隔符连接起来。
const arr = [1, 2, 3];
const str = arr.join('-');
console.log(str); // '1-2-3'
9. reverse():将数组中的元素顺序反转。
const arr = [1, 2, 3];
arr.reverse();
console.log(arr); // [3, 2, 1]
10. sort():将数组中的元素按照指定的顺序排序。
const arr = [3, 1, 4, 2, 5];
arr.sort((a, b) => a - b);
console.log(arr); // [1, 2, 3, 4, 5]
11. find():返回数组中第一个符合条件的元素,如果没有符合条件的元素则返回undefined。
const arr = [1, 2, 3, 4, 5];
const result = arr.find(item => item > 3);
console.log(result); // 4
12. findIndex():返回数组中第一个符合条件的元素的索引,如果没有符合条件的元素则返回-1。
const arr = [1, 2, 3, 4, 5];
const index = arr.findIndex(item => item > 3);
console.log(index); // 3
13. includes():判断数组中是否包含指定的元素,返回布尔值。
const arr = [1, 2, 3, 4, 5];
const result1 = arr.includes(3);
const result2 = arr.includes(6);
console.log(result1); // true
console.log(result2); // false
14. flat():将多维数组转换为一维数组。
const arr = [1, [2, 3], [4, [5, 6]]];
const newArr = arr.flat();
console.log(newArr); // [1, 2, 3, 4, 5, 6]
15. flatMap():对数组中的每个元素执行一个函数,然后将结果压缩成一个新数组。
const arr = [1, 2, 3];
const newArr = arr.flatMap(item => [item * 2]);
console.log(newArr); // [2, 4, 6]
16. from():将类数组对象或可迭代对象转换为数组。
const str = 'hello';
const arr = Array.from(str);
console.log(arr); // ['h', 'e', 'l', 'l', 'o']
17. of():创建一个包含任意数量参数的新数组。
const arr = Array.of(1, 2, 3);
console.log(arr); // [1, 2, 3]
18. lastIndexOf():方法用于查找数组中某个元素最后一次出现的索引(位置),如果找到则返回该索引值,否则返回 -1。
const fruits = ['火龙果', '橘子', '蓝莓', '西瓜', '葡萄', '橘子'];
console.log(fruits.lastIndexOf('橘子')); //5
console.log(fruits.lastIndexOf('柚子')); //-1
19. indexOf():需要注意的是,indexOf方法只会返回第一个匹配项的位置。如果数组中存在多个相同的元素,该方法只会返回第一个元素的位置。
此外,indexOf方法还可以接受一个可选的第二个参数,用于指定从哪个位置开始查找。
const fruits = ['苹果', '蓝莓', '橘子', '西瓜', '葡萄'];
console.log(fruits.indexOf('橘子', 1)); //2 返回元素下标
console.log(fruits.indexOf('橘子', 3)); //-1 没有该元素
const arr = [1,2,3,4,5,6,7,6,6,5]; // 从下标6开始查找元素5
console.log(arr.indexOf(5,6)); //9
20. join() 方法用于将数组中的所有元素以指定的分隔符连接成一个字符串
const fruits = ['苹果', '香蕉', '橘子'];
const joinedString = fruits.join(',');
console.log(fruits); //[ '苹果', '香蕉', '橘子' ]
console.log(joinedString); //苹果,香蕉,橘子
21. forEach() 方法用于对数组中的每个元素执行一个回调函数
const fruits = ['火龙果', '蓝莓', '西瓜', '葡萄'];
fruits.forEach(fruit => {
console.log(fruit); //火龙果 蓝莓 西瓜 葡萄
});