// 使用 forEach 遍历数组中的每个元素
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(item, index, array) {
console.log(`当前元素: ${item}, 索引: ${index}, 数组: ${array}`);
});
// 简化写法,使用箭头函数
numbers.forEach((item, index, array) => {
console.log(`当前元素: ${item}, 索引: ${index}, 数组: ${array}`);
});
// 解释说明:
// forEach 是一个数组方法,用于遍历数组中的每个元素。
// 它接受一个回调函数作为参数,该回调函数会在数组的每个元素上执行一次。
// 回调函数有三个参数:当前元素、当前索引和整个数组。
// forEach 不会改变原始数组,并且没有返回值。
上一篇:js websocket框架
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站