lodash forEach

本文深入探讨lodash库中的forEach方法,该方法用于遍历数组或对象的每个元素,并对其执行指定的函数。通过阅读,您将了解如何使用此功能进行迭代操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

_.forEach(collection, [iteratee=_.identity])


Iterates over elements of collection invoking iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false

Note: As with other "Collections" methods, objects with a "length" property are iterated like arrays. To avoid this behavior use _.forIn or _.forOwn for object iteration.

Arguments
  1. collection (Array|Object): The collection to iterate over.
  2. [iteratee=_.identity] (Function): The function invoked per iteration.
Returns

(*): Returns collection.

_([1, 2]).forEach(function(value) {
  console.log(value);
});
// → Logs `1` then `2`.

_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  console.log(key);
});
// → Logs 'a' then 'b' (iteration order is not guaranteed).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值