-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathforEach.js
38 lines (36 loc) · 1.56 KB
/
forEach.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import arrayEach from '../internal/arrayEach';
import baseEach from '../internal/baseEach';
import bindCallback from '../internal/bindCallback';
import isArray from '../lang/isArray';
/**
* Iterates over elements of `collection` invoking `iteratee` for each element.
* The `iteratee` is bound to `thisArg` and invoked with three arguments;
* (value, index|key, collection). Iterator 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 `_.forIn` or `_.forOwn`
* may be used for object iteration.
*
* @static
* @memberOf _
* @alias each
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array|Object|string} Returns `collection`.
* @example
*
* _([1, 2, 3]).forEach(function(n) { console.log(n); });
* // => logs each value from left to right and returns the array
*
* _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(n, key) { console.log(n, key); });
* // => logs each value-key pair and returns the object (iteration order is not guaranteed)
*/
function forEach(collection, iteratee, thisArg) {
return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection))
? arrayEach(collection, iteratee)
: baseEach(collection, bindCallback(iteratee, thisArg, 3));
}
export default forEach;