Interpretation of angular forEach method traversal source code

  • 2021-07-15 03:37:38
  • OfStack

The forEach () method is provided in angular for traversing objects or arrays for your reference, as follows


function forEach(obj, iterator, context) {
 var key, length;
 if (obj) {
  if (isFunction(obj)) {
   for (key in obj) {
    // Need to check if hasOwnProperty exists,
    // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
    if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  } else if (isArray(obj) || isArrayLike(obj)) {
   var isPrimitive = typeof obj !== 'object';
   for (key = 0, length = obj.length; key < length; key++) {
    if (isPrimitive || key in obj) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  } else if (obj.forEach && obj.forEach !== forEach) {
    obj.forEach(iterator, context, obj);
  } else if (isBlankObject(obj)) {
   // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
   for (key in obj) {
    iterator.call(context, obj[key], key, obj);
   }
  } else if (typeof obj.hasOwnProperty === 'function') {
   // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed
   for (key in obj) {
    if (obj.hasOwnProperty(key)) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  } else {
   // Slow path for objects which do not have a method `hasOwnProperty`
   for (key in obj) {
    if (hasOwnProperty.call(obj, key)) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  }
 }
 return obj;
}

Official description:

The forEach method can traverse arrays or objects, and the function has three arguments: value, key, and obj.
1), value value refers to the current value of the object or array element that is traversed
2), key is the key of the object property or the index of the array
3) obj obj is the object or array itself that is traversed

Example:


   var values = {name: 'misko', gender: 'male'};
   var log = [];
   angular.forEach(values, function(value, key) {
    this.push(key + ': ' + value);
   }, log);

Related articles: