// 使用 .forEach 方法遍历数组并执行回调函数
const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach(function(fruit, index, array) {
console.log(`Index ${index}: ${fruit}`);
});
// 输出:
// Index 0: apple
// Index 1: banana
// Index 2: cherry
// 解释说明:
// .forEach 是一个数组方法,用于遍历数组中的每个元素,并对每个元素执行提供的函数。
// 回调函数接受三个参数:当前元素、当前索引和整个数组。
// 注意:.forEach 不会返回新数组,它只是对每个元素执行操作。
上一篇:js 在线调试
下一篇:js xpath
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站