* @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, key, obj)`, where `value` *
(obj, iterator, context)
| 393 | */ |
| 394 | |
| 395 | function forEach(obj, iterator, context) { |
| 396 | var key, length; |
| 397 | if (obj) { |
| 398 | if (isFunction(obj)) { |
| 399 | for (key in obj) { |
| 400 | if (key !== 'prototype' && key !== 'length' && key !== 'name' && obj.hasOwnProperty(key)) { |
| 401 | iterator.call(context, obj[key], key, obj); |
| 402 | } |
| 403 | } |
| 404 | } else if (isArray(obj) || isArrayLike(obj)) { |
| 405 | var isPrimitive = typeof obj !== 'object'; |
| 406 | for (key = 0, length = obj.length; key < length; key++) { |
| 407 | if (isPrimitive || key in obj) { |
| 408 | iterator.call(context, obj[key], key, obj); |
| 409 | } |
| 410 | } |
| 411 | } else if (obj.forEach && obj.forEach !== forEach) { |
| 412 | obj.forEach(iterator, context, obj); |
| 413 | } else if (isBlankObject(obj)) { |
| 414 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 415 | for (key in obj) { |
| 416 | iterator.call(context, obj[key], key, obj); |
| 417 | } |
| 418 | } else if (typeof obj.hasOwnProperty === 'function') { |
| 419 | // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed |
| 420 | for (key in obj) { |
| 421 | if (obj.hasOwnProperty(key)) { |
| 422 | iterator.call(context, obj[key], key, obj); |
| 423 | } |
| 424 | } |
| 425 | } else { |
| 426 | // Slow path for objects which do not have a method `hasOwnProperty` |
| 427 | for (key in obj) { |
| 428 | if (hasOwnProperty.call(obj, key)) { |
| 429 | iterator.call(context, obj[key], key, obj); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return obj; |
| 435 | } |
| 436 | |
| 437 | function forEachSorted(obj, iterator, context) { |
| 438 | var keys = Object.keys(obj).sort(); |
no test coverage detected