* @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)
| 324 | */ |
| 325 | |
| 326 | function forEach(obj, iterator, context) { |
| 327 | var key, length; |
| 328 | if (obj) { |
| 329 | if (isFunction(obj)) { |
| 330 | for (key in obj) { |
| 331 | // Need to check if hasOwnProperty exists, |
| 332 | // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function |
| 333 | if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) { |
| 334 | iterator.call(context, obj[key], key, obj); |
| 335 | } |
| 336 | } |
| 337 | } else if (isArray(obj) || isArrayLike(obj)) { |
| 338 | var isPrimitive = typeof obj !== 'object'; |
| 339 | for (key = 0, length = obj.length; key < length; key++) { |
| 340 | if (isPrimitive || key in obj) { |
| 341 | iterator.call(context, obj[key], key, obj); |
| 342 | } |
| 343 | } |
| 344 | } else if (obj.forEach && obj.forEach !== forEach) { |
| 345 | obj.forEach(iterator, context, obj); |
| 346 | } else if (isBlankObject(obj)) { |
| 347 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 348 | for (key in obj) { |
| 349 | iterator.call(context, obj[key], key, obj); |
| 350 | } |
| 351 | } else if (typeof obj.hasOwnProperty === 'function') { |
| 352 | // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed |
| 353 | for (key in obj) { |
| 354 | if (obj.hasOwnProperty(key)) { |
| 355 | iterator.call(context, obj[key], key, obj); |
| 356 | } |
| 357 | } |
| 358 | } else { |
| 359 | // Slow path for objects which do not have a method `hasOwnProperty` |
| 360 | for (key in obj) { |
| 361 | if (hasOwnProperty.call(obj, key)) { |
| 362 | iterator.call(context, obj[key], key, obj); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | return obj; |
| 368 | } |
| 369 | |
| 370 | function forEachSorted(obj, iterator, context) { |
| 371 | var keys = Object.keys(obj).sort(); |
no test coverage detected