// 使用数组的 forEach 方法遍历数组元素
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(element, index, array) {
console.log(`元素: ${element}, 索引: ${index}, 数组: ${array}`);
});
// 解释:
// forEach 是数组的一个方法,用于遍历数组中的每个元素。
// 它接受一个回调函数作为参数,该回调函数会在数组的每个元素上执行。
// 回调函数有三个参数:
// - element: 当前元素的值
// - index: 当前元素的索引
// - array: 被遍历的数组本身
上一篇:js 截取字符串前几位
下一篇:js 判断数组类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站