* @ngdoc function * @name angular.forEach * @module ng * @kind function * * @description * Invokes the `iterator` function once for each item in `obj` collection, which can be either an * object or an array. The `iterator` function is invoked with `iterator(value,
(obj, iterator, context)
| 404 | */ |
| 405 | |
| 406 | function forEach(obj, iterator, context) { |
| 407 | var key, length; |
| 408 | if (obj) { |
| 409 | if (isFunction(obj)) { |
| 410 | for (key in obj) { |
| 411 | if (key !== 'prototype' && key !== 'length' && key !== 'name' && obj.hasOwnProperty(key)) { |
| 412 | iterator.call(context, obj[key], key, obj); |
| 413 | } |
| 414 | } |
| 415 | } else if (isArray(obj) || isArrayLike(obj)) { |
| 416 | var isPrimitive = typeof obj !== 'object'; |
| 417 | for (key = 0, length = obj.length; key < length; key++) { |
| 418 | if (isPrimitive || key in obj) { |
| 419 | iterator.call(context, obj[key], key, obj); |
| 420 | } |
| 421 | } |
| 422 | } else if (obj.forEach && obj.forEach !== forEach) { |
| 423 | obj.forEach(iterator, context, obj); |
| 424 | } else if (isBlankObject(obj)) { |
| 425 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 426 | for (key in obj) { |
| 427 | iterator.call(context, obj[key], key, obj); |
| 428 | } |
| 429 | } else if (typeof obj.hasOwnProperty === 'function') { |
| 430 | // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed |
| 431 | for (key in obj) { |
| 432 | if (obj.hasOwnProperty(key)) { |
| 433 | iterator.call(context, obj[key], key, obj); |
| 434 | } |
| 435 | } |
| 436 | } else { |
| 437 | // Slow path for objects which do not have a method `hasOwnProperty` |
| 438 | for (key in obj) { |
| 439 | if (hasOwnProperty.call(obj, key)) { |
| 440 | iterator.call(context, obj[key], key, obj); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | return obj; |
| 446 | } |
| 447 | |
| 448 | function forEachSorted(obj, iterator, context) { |
| 449 | var keys = Object.keys(obj).sort(); |
no test coverage detected