forEach.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. var arrayEach = require('../internal/arrayEach'),
  2. baseEach = require('../internal/baseEach'),
  3. createForEach = require('../internal/createForEach');
  4. /**
  5. * Iterates over elements of `collection` invoking `iteratee` for each element.
  6. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  7. * (value, index|key, collection). Iteratee functions may exit iteration early
  8. * by explicitly returning `false`.
  9. *
  10. * **Note:** As with other "Collections" methods, objects with a "length" property
  11. * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
  12. * may be used for object iteration.
  13. *
  14. * @static
  15. * @memberOf _
  16. * @alias each
  17. * @category Collection
  18. * @param {Array|Object|string} collection The collection to iterate over.
  19. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  20. * @param {*} [thisArg] The `this` binding of `iteratee`.
  21. * @returns {Array|Object|string} Returns `collection`.
  22. * @example
  23. *
  24. * _([1, 2]).forEach(function(n) {
  25. * console.log(n);
  26. * }).value();
  27. * // => logs each value from left to right and returns the array
  28. *
  29. * _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
  30. * console.log(n, key);
  31. * });
  32. * // => logs each value-key pair and returns the object (iteration order is not guaranteed)
  33. */
  34. var forEach = createForEach(arrayEach, baseEach);
  35. module.exports = forEach;