each.js 719 B

1234567891011121314151617181920212223
  1. import optimizeCb from './_optimizeCb.js';
  2. import isArrayLike from './_isArrayLike.js';
  3. import keys from './keys.js';
  4. // The cornerstone for collection functions, an `each`
  5. // implementation, aka `forEach`.
  6. // Handles raw objects in addition to array-likes. Treats all
  7. // sparse array-likes as if they were dense.
  8. export default function each(obj, iteratee, context) {
  9. iteratee = optimizeCb(iteratee, context);
  10. var i, length;
  11. if (isArrayLike(obj)) {
  12. for (i = 0, length = obj.length; i < length; i++) {
  13. iteratee(obj[i], i, obj);
  14. }
  15. } else {
  16. var _keys = keys(obj);
  17. for (i = 0, length = _keys.length; i < length; i++) {
  18. iteratee(obj[_keys[i]], _keys[i], obj);
  19. }
  20. }
  21. return obj;
  22. }