Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js each

作者:△冷色彩   发布日期:2026-04-05   浏览:85

// 使用 Array.prototype.forEach 方法遍历数组
const array = [1, 2, 3, 4, 5];

array.forEach(function(element, index, arr) {
    console.log(`元素: ${element}, 索引: ${index}, 数组: ${arr}`);
});

// 解释:
// forEach 是 JavaScript 中用于遍历数组的方法。
// 它接受一个回调函数作为参数,该回调函数会在数组的每个元素上执行。
// 回调函数有三个参数:当前元素、当前索引和整个数组。

如果你需要更简洁的箭头函数版本:

const array = [1, 2, 3, 4, 5];

array.forEach((element, index, arr) => {
    console.log(`元素: ${element}, 索引: ${index}, 数组: ${arr}`);
});

上一篇:js for each

下一篇:js foreach()

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站