Document directory
- Aliases
- Arguments
- Returns
- Aliases
- Arguments
- Returns
_. Each
Traverses the set and calls back each element in the set.
APILo-Dash
_. ForEach (collection [, callback = identity, thisArg])
Aliases
Each
Arguments
collection
(Array | Object | String): Set to be traversed
[callback=identity]
(Function): Functions called in each iteration
[thisArg]
(Arbitrary): Bindcallback
Ofthis
callback
Three parameters are accepted:(value, index|key, collection)
Returns
(Array, Object, String): Returncollection
.
Underscore
_. Each (list, iterator, [context])
Aliases
ForEach
Arguments
list
(Array | Object | String): Set to be traversed
iterator
(Function): Functions called in each iteration
[context]
(Arbitrary): Bindcallback
Ofthis
iterator
Three parameters are accepted:(element|value, index|key, list)
Returns
(Array, Object, String): Returnundefined
.
Note
- Lo-Dash can omit the callback function, while Underscore must be passed in
- Lo-Dash can be returned through callback
false
End iteration ahead of schedule
- Lo-Dash returns the Collection to allow chained operations. The returned value of Underscore is undefined.
ExampleLo-Dash
12345678 |
_. ForEach ([1, 2]) // => Returns [1, 2, 3] _ ([1, 2, 3]). forEach (alert ). join (','); // => alert returns '1, 2, 3 '_ for each number '_. forEach ({'one': 1, 'two': 2, 'three ': 3}, alert ); // => alert each number value (not necessarily executed in the defined order) |
Underscore
1234 |
_. Each ([1, 2, 3], alert); // => alert each number _. each ({one: 1, two: 2, three: 3}, alert); // => alert each number value (not necessarily in the defined order) |
SourceLo-Dash
12345678910111213141516 |
function forEach(collection, callback, thisArg) { var index = -1, length = collection ? collection.length : 0; callback = callback && typeof thisArg == 'undefined' ? callback : lodash.createCallback(callback, thisArg); if (typeof length == 'number') { while (++index < length) { if (callback(collection[index], index, collection) === false) { break; } } } else { forOwn(collection, callback); } return collection;} |
Underscore
12345678910111213141516 |
var each = _.each = _.forEach = function(obj, iterator, context) { if (obj == null) return; if (nativeForEach && obj.forEach === nativeForEach) { obj.forEach(iterator, context); } else if (obj.length === +obj.length) { for (var i = 0, l = obj.length; i < l; i++) { if (iterator.call(context, obj[i], i, obj) === breaker) return; } } else { for (var key in obj) { if (_.has(obj, key)) { if (iterator.call(context, obj[key], key, obj) === breaker) return; } } } }; |
Additional
obj.length === +obj.length
+obj
: Convert obj to a decimal number; otherwise, returnNaN
. Therefore, the above judgment is equivalentobj.length && typeof obj.length == 'number'
if (iterator.call(context, obj[i], i, obj) === breaker) return;
breaker
Is a pre-defined empty object ({}). Underscore is used internally to end the cycle in advance and is not made public to the public. In addition, because the object's===
The object address is compared, so even if the user returns{}
if
Still not valid
for in
Loop does not traverse the non-enumerable attribute, so likeObject
OftoString
Will not be iterated