// 使用 JavaScript 的 forEach 方法遍历数组
// 定义一个数组
const fruits = ['apple', 'banana', 'cherry'];
// 使用 forEach 方法遍历数组中的每个元素
fruits.forEach(function(fruit, index, array) {
console.log(`索引 ${index}: ${fruit}`);
// fruit 是当前元素
// index 是当前元素的索引
// array 是被遍历的数组本身
});
// 输出结果:
// 索引 0: apple
// 索引 1: banana
// 索引 2: cherry
forEach 是 JavaScript 中用于遍历数组的方法。fruit: 当前元素。index: 当前元素的索引。array: 正在被遍历的数组(通常不需要使用)。forEach 不会返回任何值,它只是对数组中的每个元素执行指定的操作。上一篇:js foreach 退出循环
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站