Array.each
Array.each
功能
遍历数组并执行回调。
语法
array.each(callback)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| callback | Fn | 否 | Empty | 回调签名为 fn(value, index, current)。 |
返回值
| 类型 | 说明 |
|---|---|
| Array | 返回原数组,便于继续链式调用。 |
示例
items = [1, 2, 3] sum = 0 items.each(fn(value) { sum += value }) // 输出:6 echo(sum)
注意事项
- 遍历源是调用时数组的浅拷贝。
- 回调不是函数时不会报错,回调结果按 Null 处理。