ES6数组语法15个总结:前端从业者必备宝典
2023-11-09 04:23:19
- Array.from()
Array.from()
方法用于将类数组对象或可迭代对象转换为真正的数组。
const arrayLike = {
0: 'a',
1: 'b',
2: 'c',
length: 3
};
const array = Array.from(arrayLike);
console.log(array); // ['a', 'b', 'c']
2. Array.of()
Array.of()
方法用于创建具有固定长度的数组,并且数组中的每个元素都是相同的值。
const array = Array.of(1, 2, 3);
console.log(array); // [1, 2, 3]
3. Array.prototype.find()
Array.prototype.find()
方法用于在数组中找到第一个满足条件的元素,并返回该元素。
const array = [1, 2, 3, 4, 5];
const found = array.find(element => element > 3);
console.log(found); // 4
4. Array.prototype.findIndex()
Array.prototype.findIndex()
方法用于在数组中找到第一个满足条件的元素的索引,并返回该索引。
const array = [1, 2, 3, 4, 5];
const index = array.findIndex(element => element > 3);
console.log(index); // 3
5. Array.prototype.forEach()
Array.prototype.forEach()
方法用于对数组中的每个元素执行指定的回调函数。
const array = [1, 2, 3, 4, 5];
array.forEach(element => {
console.log(element);
});
// 输出:
// 1
// 2
// 3
// 4
// 5
6. Array.prototype.map()
Array.prototype.map()
方法用于对数组中的每个元素执行指定的回调函数,并返回一个新数组,新数组中的每个元素都是回调函数的返回值。
const array = [1, 2, 3, 4, 5];
const doubledArray = array.map(element => element * 2);
console.log(doubledArray); // [2, 4, 6, 8, 10]
7. Array.prototype.filter()
Array.prototype.filter()
方法用于对数组中的每个元素执行指定的回调函数,并返回一个新数组,新数组中的每个元素都是回调函数返回true的元素。
const array = [1, 2, 3, 4, 5];
const evenArray = array.filter(element => element % 2 === 0);
console.log(evenArray); // [2, 4]
8. Array.prototype.some()
Array.prototype.some()
方法用于确定数组中是否有至少一个元素满足指定的回调函数。
const array = [1, 2, 3, 4, 5];
const hasEvenNumber = array.some(element => element % 2 === 0);
console.log(hasEvenNumber); // true
9. Array.prototype.every()
Array.prototype.every()
方法用于确定数组中的所有元素是否都满足指定的回调函数。
const array = [1, 2, 3, 4, 5];
const allEvenNumbers = array.every(element => element % 2 === 0);
console.log(allEvenNumbers); // false
10. Array.prototype.reduce()
Array.prototype.reduce()
方法用于将数组中的所有元素归并为一个值。
const array = [1, 2, 3, 4, 5];
const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 15
11. Array.prototype.reduceRight()
Array.prototype.reduceRight()
方法与Array.prototype.reduce()
方法类似,但它从数组的最后一个元素开始归并,并向左移动。
const array = [1, 2, 3, 4, 5];
const sum = array.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 15
12. Array.prototype.concat()
Array.prototype.concat()
方法用于将两个或多个数组合并为一个新数组。
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const newArray = array1.concat(array2);
console.log(newArray); // [1, 2, 3, 4, 5, 6]
13. Array.prototype.join()
Array.prototype.join()
方法用于将数组中的所有元素连接成一个字符串。
const array = [1, 2, 3];
const joinedString = array.join(',');
console.log(joinedString); // "1,2,3"
14. Array.prototype.slice()
Array.prototype.slice()
方法用于从数组中提取一个子数组。
const array = [1, 2, 3, 4, 5];
const subArray = array.slice(1, 3);
console.log(subArray); // [2, 3]
15. Array.prototype.sort()
Array.prototype.sort()
方法用于对数组中的元素进行排序。
const array = [1, 5, 3, 2, 4];
array.sort();
console.log(array); // [1, 2, 3, 4, 5]
总结
ES6数组语法非常丰富且强大,可以帮助你更高效地处理数组数据。希望通过本文的总结,你能对这些语法有一个更深入的了解,并能够在实际开发中熟练地使用它们。