目录
1. concat(): 连接两个或多个数组,并返回一个新的数组。
2. copyWithin(): 复制数组中一系列元素到同一数组指定的起始位置。
3. entries(): 生成一个包含数组中每个索引键值对的迭代器对象。
4. every(): 用于检测数组所有元素是否都符合指定条件。
5. fill(): 用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。
8. findIndex(): 返回数组中符合条件的第一个元素索引。
9. flat(): 将嵌套的数组“扁平化”,即将所有子数组中的元素合并到一个新数组中。
10. flatMap(): 对数组中的每个元素执行一个映射函数,并将结果“扁平化”。
11. forEach(): 数组每个元素都执行一次提供的函数。
13. includes(): 判断数组是否包含某个值,返回布尔值。
14. indexOf(): 返回在数组中可以找到一个给定元素的第一个索引。
18. lastIndexOf(): 返回在数组中可以找到一个给定元素的最后一个索引。
20. Arroy.of(): 创建一个由任意数量参数组成的新数组实例。
22. push(): 将一个或多个元素添加到数组的末尾,并返回数组的新长度。
23. reduce(): 从左到右累加器处理数组的每个值。
24. reduceRight(): 从右到左累加器处理数组的每个值。
27. slice(): 选取数组的一部分,并返回一个新数组。
28. some(): 用于检测数组中是否存在满足条件的元素。
31. toLocaleString(): 数组转换为本地字符串。
33. unshift(): 在数组的开头添加一个或更多元素,并返回新的长度。
一、简介:
JavaScript数组的所有方法,包括ES中引入的部分方法。
二、使用示例:
1. concat(): 连接两个或多个数组,并返回一个新的数组。
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const newArr = arr1.concat(arr2);
console.log(newArr); // 输出: [1, 2, 3, 4, 5, 6]
2. copyWithin(): 复制数组中一系列元素到同一数组指定的起始位置。
const arr = ['a', 'b', 'c', 'd', 'e'];
arr.copyWithin(0, 3, 5); // 从索引3开始复制,复制到索引5之前的位置,然后粘贴到索引0的位置。
console.log(arr); // 输出: ['d', 'e', 'c', 'd', 'e']
3. entries(): 生成一个包含数组中每个索引键值对的迭代器对象。
const arr = ['a', 'b', 'c'];
const iterator = arr.entries();
console.log(iterator.next().value); // 输出: [0, 'a']
console.log(iterator.next().value); // 输出: [1, 'b']
console.log(iterator.next().value); // 输出: [2, 'c']
4. every(): 用于检测数组所有元素是否都符合指定条件。
const arr = [1, 2, 3, 4, 5];
const allPositive = arr.every(function(num) {
return num > 0;
});
console.log(allPositive); // 输出: true
5. fill(): 用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。
const arr = [1, 2, 3, 4, 5];
arr.fill(0, 2, 4); // 从索引2到4之间的元素都被替换为0
console.log(arr); // 输出: [1, 2, 0, 0, 5]
6. filter(): 返回符合条件的数组元素。
const arr = [1, 2, 3, 4, 5];
const filteredArr = arr.filter(function(num) {
return num % 2 === 0; // 返回偶数
});
console.log(filteredArr); // 输出: [2, 4]
7. find(): 返回数组中符合条件的第一个元素。
const arr = [1, 2, 3, 4, 5];
const firstEvenNum = arr.find(function(num) {
return num % 2 === 0;
});
console.log(firstEvenNum); // 输出: 2
8. findIndex(): 返回数组中符合条件的第一个元素索引。
const arr = [1, 2, 3, 4, 5];
const firstEvenIndex = arr.findIndex(function(num) {
return num % 2 === 0;
});
console.log(firstEvenIndex); // 输出: 1
9. flat(): 将嵌套的数组“扁平化”,即将所有子数组中的元素合并到一个新数组中。
const arr = [1, 2, [3, 4], [5, [6, 7]]];
const flattenedArr = arr.flat();
console.log(flattenedArr); // 输出: [1, 2, 3, 4, 5, [6, 7]]
10. flatMap(): 对数组中的每个元素执行一个映射函数,并将结果“扁平化”。
const arr = [1, 2, 3];
const doubledAndSquared = arr.flatMap(function(num) {
return [num * 2, num * num];
});
console.log(doubledAndSquared); // 输出: [2, 1, 4, 4, 6, 9]
11. forEach(): 数组每个元素都执行一次提供的函数。
const arr = [1, 2, 3];
arr.forEach(function(num) {
console.log(num);
}); // 输出: 1, 2, 3
12. from(): 通过给定的对象创建一个数组。
const str = 'hello';
const arr = Array.from(str);
console.log(arr); // 输出: ['h', 'e', 'l', 'l', 'o']
13. includes(): 判断数组是否包含某个值,返回布尔值。
const arr = [1, 2, 3];
const includesTwo = arr.includes(2);
console.log(includesTwo); // 输出: true
14. indexOf(): 返回在数组中可以找到一个给定元素的第一个索引。
const arr = [1, 2, 3, 4, 5];
const index = arr.indexOf(3);
console.log(index); // 输出: 2
15. isArray(): 判断一个对象是否为数组。
const arr = [1, 2, 3];
const isArr = Array.isArray(arr);
console.log(isArr); // 输出: true
16. join(): 连接所有数组元素并返回字符串。
const arr = ['hello', 'world'];
const str = arr.join(' ');
console.log(str); // 输出: 'hello world'
17. keys(): 返回数组中每个索引的键名。
const arr = ['a', 'b', 'c'];
const iterator = arr.keys();
console.log(iterator.next().value); // 输出: 0
console.log(iterator.next().value); // 输出: 1
console.log(iterator.next().value); // 输出: 2
18. lastIndexOf(): 返回在数组中可以找到一个给定元素的最后一个索引。
const arr = [1, 2, 3, 4, 2];
const lastIndex = arr.lastIndexOf(2);
console.log(lastIndex); // 输出: 4
19. map(): 返回由回调函数的返回值组成的新数组。
const arr = [1, 2, 3];
const squaredArr = arr.map(function(num) {
return num * num;
});
console.log(squaredArr); // 输出: [1, 4, 9]
20. Arroy.of(): 创建一个由任意数量参数组成的新数组实例。
const arr = Array.of(1, 2, 3);
console.log(arr); // 输出: [1, 2, 3]
21. pop(): 删除并返回数组的最后一个元素。
const arr = [1, 2, 3];
const lastNum = arr.pop();
console.log(lastNum); // 输出: 3
console.log(arr); // 输出: [1, 2]
22. push(): 将一个或多个元素添加到数组的末尾,并返回数组的新长度。
const arr = [1, 2, 3];
const newLength = arr.push(4, 5);
console.log(newLength); // 输出: 5
console.log(arr); // 输出: [1, 2, 3, 4, 5]
23. reduce(): 从左到右累加器处理数组的每个值。
const arr = [1, 2, 3];
const sum = arr.reduce(function(acc, curr) {
return acc + curr;
}, 0);
console.log(sum); // 输出: 6
24. reduceRight(): 从右到左累加器处理数组的每个值。
const arr = [1, 2, 3];
const reversedStr = arr.reduceRight(function(acc, curr) {
return acc + curr;
}, '');
console.log(reversedStr); // 输出: '321'
25. reverse(): 反转数组的顺序。
const arr = [1, 2, 3];
arr.reverse();
console.log(arr); // 输出: [3, 2, 1]
26. shift(): 删除并返回数组的第一个元素。
const arr = [1, 2, 3];
const firstNum = arr.shift();
console.log(firstNum); // 输出: 1
console.log(arr); // 输出: [2, 3]
27. slice(): 选取数组的一部分,并返回一个新数组。
const arr = [1, 2, 3, 4, 5];
const slicedArr = arr.slice(1, 4); // 选取索引1到4之间的元素(不包括4)
console.log(slicedArr); // 输出: [2, 3, 4]
28. some(): 用于检测数组中是否存在满足条件的元素。
const arr = [1, 2, 3, 4, 5];
const hasEvenNum = arr.some(function(num) {
return num % 2 === 0;
});
console.log(hasEvenNum); // 输出: true
29. sort(): 对数组进行排序并返回排序后的数组。
const arr = [3, 1, 4, 2, 5];
arr.sort();
console.log(arr); // 输出: [1, 2, 3, 4, 5]
30. splice(): 从数组中添加或删除元素。
const arr = [1, 2, 3, 4, 5];
arr.splice(2, 2, 'a', 'b'); // 从索引2开始删除两个元素,然后插入'a'和'b'
console.log(arr); // 输出: [1, 2, 'a', 'b', 5]
31. toLocaleString(): 数组转换为本地字符串。
const arr = [1, 2, 3];
const str = arr.toLocaleString();
console.log(str); // 输出: '1,2,3'
32. toString(): 把数组转换为字符串。
const arr = [1, 2, 3];
const str = arr.toString();
console.log(str); // 输出: '1,2,3'
33. unshift(): 在数组的开头添加一个或更多元素,并返回新的长度。
const arr = [1, 2, 3];
const newLength = arr.unshift(0, -1);
console.log(newLength); // 输出: 5
console.log(arr); // 输出: [-1, 0, 1, 2, 3]
34. values(): 返回数组中每个索引的键值。
const arr = ['a', 'b', 'c'];
const iterator = arr.values();
console.log(iterator.next().value); // 输出: 'a'
console.log(iterator.next().value); // 输出: 'b'
console.log(iterator.next().value); // 输出: 'c'
如有问题,欢迎指正~
欢迎大家一起补充 ~