* @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)
| 308 | */ |
| 309 | |
| 310 | function forEach(obj, iterator, context) { |
| 311 | var key, length; |
| 312 | if (obj) { |
| 313 | if (isFunction(obj)) { |
| 314 | for (key in obj) { |
| 315 | // Need to check if hasOwnProperty exists, |
| 316 | // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function |
| 317 | if (key !== 'prototype' && key !== 'length' && key !== 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) { |
| 318 | iterator.call(context, obj[key], key, obj); |
| 319 | } |
| 320 | } |
| 321 | } else if (isArray(obj) || isArrayLike(obj)) { |
| 322 | var isPrimitive = typeof obj !== 'object'; |
| 323 | for (key = 0, length = obj.length; key < length; key++) { |
| 324 | if (isPrimitive || key in obj) { |
| 325 | iterator.call(context, obj[key], key, obj); |
| 326 | } |
| 327 | } |
| 328 | } else if (obj.forEach && obj.forEach !== forEach) { |
| 329 | obj.forEach(iterator, context, obj); |
| 330 | } else if (isBlankObject(obj)) { |
| 331 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 332 | for (key in obj) { |
| 333 | iterator.call(context, obj[key], key, obj); |
| 334 | } |
| 335 | } else if (typeof obj.hasOwnProperty === 'function') { |
| 336 | // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed |
| 337 | for (key in obj) { |
| 338 | if (obj.hasOwnProperty(key)) { |
| 339 | iterator.call(context, obj[key], key, obj); |
| 340 | } |
| 341 | } |
| 342 | } else { |
| 343 | // Slow path for objects which do not have a method `hasOwnProperty` |
| 344 | for (key in obj) { |
| 345 | if (hasOwnProperty.call(obj, key)) { |
| 346 | iterator.call(context, obj[key], key, obj); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | return obj; |
| 352 | } |
| 353 | |
| 354 | function forEachSorted(obj, iterator, context) { |
| 355 | var keys = Object.keys(obj).sort(); |
no test coverage detected