// 使用forEach方法遍历数组并获取索引
const array = ['apple', 'banana', 'cherry'];
array.forEach((item, index) => {
console.log(`Index: ${index}, Item: ${item}`);
});
// 输出:
// Index: 0, Item: apple
// Index: 1, Item: banana
// Index: 2, Item: cherry
forEach
是 JavaScript 中用于遍历数组的方法。item
: 当前元素的值。index
: 当前元素的索引。array
: 正在遍历的数组(可选)。item
和 index
参数来输出每个元素及其对应的索引。下一篇:js 获取数组下标
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站