es6中遍历数组的方法有以下几种: for...of循环:通过for...of循环可以依次遍历数组中的每个元素。例如: cons
const arr = [1, 2, 3];
for (let element of arr) {
console.log(element);
}
const arr = [1, 2, 3];
arr.forEach(function(element) {
console.log(element);
});
const arr = [1, 2, 3];
const newArr = arr.map(function(element) {
return element * 2;
});
console.log(newArr); // [2, 4, 6]
const arr = [1, 2, 3, 4, 5];
const newArr = arr.filter(function(element) {
return element % 2 === 0;
});
console.log(newArr); // [2, 4]
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum); // 15
const arr = [1, 2, 3, 4, 5];
const result = arr.find(function(element) {
return element > 3;
});
console.log(result); // 4
const arr = [1, 2, 3, 4, 5];
const index = arr.findIndex(function(element) {
return element > 3;
});
console.log(index); // 3
const arr = [1, 2, 3, 4, 5];
const result = arr.some(function(element) {
return element > 3;
});
console.log(result); // true
const arr = [1, 2, 3, 4, 5];
const result = arr.every(function(element) {
return element > 0;
});
console.log(result); // true
--结束END--
本文标题: es6遍历数组的方法有哪些
本文链接: https://lsjlt.com/news/450625.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0