// 使用 Array.prototype.forEach 方法遍历数组
const array = [1, 2, 3, 4, 5];
array.forEach(function(element, index, arr) {
console.log(`元素: ${element}, 索引: ${index}, 数组: ${arr}`);
});
// 解释:
// forEach 是 JavaScript 中用于遍历数组的方法。
// 它接受一个回调函数作为参数,该回调函数会在数组的每个元素上执行。
// 回调函数有三个参数:当前元素、当前索引和整个数组。
如果你需要更简洁的箭头函数版本:
const array = [1, 2, 3, 4, 5];
array.forEach((element, index, arr) => {
console.log(`元素: ${element}, 索引: ${index}, 数组: ${arr}`);
});
上一篇:js for each
下一篇:js foreach()
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站