forEach
forEach()
方法用于遍历数组中的每个元素,对每个元素执行指定的操作。与 map()
不同的是,forEach()
不会返回一个新的数组,而是直接对原始数组进行操作。
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(num) {
console.log(num);
});
// Output:
// 1
// 2
// 3
// 4
// 5
forEach()
方法用于遍历数组中的每个元素,对每个元素执行指定的操作。与 map()
不同的是,forEach()
不会返回一个新的数组,而是直接对原始数组进行操作。
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(num) {
console.log(num);
});
// Output:
// 1
// 2
// 3
// 4
// 5