lodash forEach

来源:互联网 发布:中小企业办公软件 编辑:程序博客网 时间:2024/05/13 12:39

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


Iterates over elements of collection invoking iteratee foreach element. The iteratee is invoked with threearguments: (value,index|key, collection).Iteratee functions may exit iteration early by explicitlyreturning false

Note: Aswith other "Collections" methods, objects with a "length" propertyare iterated like arrays. To avoid this behavioruse _.forIn or _.forOwn forobject 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).
0 0
原创粉丝点击