_.forEach(collection, [iteratee=_.identity])

75

_.forEach(collection, [iteratee=_.identity])

_.forEach类似原生forEach,遍历集合的每一个元素,然后调用iteratee方法,对象用forIn或forOwn方法遍历

参数

collection (Array|Object): 需要遍历的集合
[iteratee=_.identity] (Function): 遍历的每个元素都调用的方法

返回值

(*): 返回原集合

例子

_.forEach([1, 2], function(value) {
  console.log(value);
});
// => Logs `1` then `2`.
 
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  console.log(key);
});
// => Logs 'a' then 'b' (iteration order is not guaranteed).

源代码:

import arrayEach from './.internal/arrayEach.js'
import baseEach from './.internal/baseEach.js'

/**
 * Iterates over elements of `collection` and invokes `iteratee` for each element.
 * The iteratee is invoked with three arguments: (value, index|key, collection).
 * Iteratee functions may exit iteration early by explicitly returning `false`.
 *
 * **Note:** As with other "Collections" methods, objects with a "length"
 * property are iterated like arrays. To avoid this behavior use `forIn`
 * or `forOwn` for object iteration.
 *
 * @since 0.1.0
 * @alias each
 * @category Collection
 * @param {Array|Object} collection The collection to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array|Object} Returns `collection`.
 * @see forEachRight, forIn, forInRight, forOwn, forOwnRight
 * @example
 *
 * forEach([1, 2], value => console.log(value))
 * // => Logs `1` then `2`.
 *
 * forEach({ 'a': 1, 'b': 2 }, (value, key) => console.log(key))
 * // => Logs 'a' then 'b' (iteration order is not guaranteed).
 */
//类似原生forEach,遍历每一个元素,对象用forIn或forOwn方法遍历
function forEach(collection, iteratee) {
  const func = Array.isArray(collection) ? arrayEach : baseEach//数组就用arrayEach方法,否则用baseEach
  return func(collection, iteratee)//调用对应方法
}

export default forEach
arrayEach
/**
 * A specialized version of `forEach` for arrays.
 *
 * @private
 * @param {Array} [array] The array to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array} Returns `array`.
 */
//数组的forEach
function arrayEach(array, iteratee) {
  let index = -1//循环索引
  const length = array == null ? 0 : array.length//数组长度

  while (++index < length) {//循环,如果iteratee返回值是false,提前跳出循环结束
    if (iteratee(array[index], index, array) === false) {
      break
    }
  }
  return array
}

export default arrayEach
baseEach
import baseForOwn from './baseForOwn.js'
import isArrayLike from '../isArrayLike.js'

/**
 * The base implementation of `forEach`.
 *
 * @private
 * @param {Array|Object} collection The collection to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array|Object} Returns `collection`.
 */
//forEach的基础实现
//collection被迭代遍历的对象,iteratee遍历器
function baseEach(collection, iteratee) {
  if (collection == null) {//如果collection为空,直接返回原对象
    return collection
  }
  if (!isArrayLike(collection)) {//如果collection不是array-like对象,就调用baseForOwn处理
    return baseForOwn(collection, iteratee)
  }
  const length = collection.length//对象长度
  const iterable = Object(collection)
  let index = -1//循环索引

  while (++index < length) {//循环调用iteratee方法,如果iteratee返回值是false,提前跳出循环结束
    if (iteratee(iterable[index], index, iterable) === false) {
      break
    }
  }
  return collection
}

export default baseEach
baseForOwn
import baseFor from './baseFor.js'
import keys from '../keys.js'

/**
 * The base implementation of `forOwn`.
 *
 * @private
 * @param {Object} object The object to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Object} Returns `object`.
 */
//forOwn方法的基础实现
function baseForOwn(object, iteratee) {
  return object && baseFor(object, iteratee, keys)//如果object不为空,调用baseFor处理
}

export default baseForOwn
baseFor
/**
 * The base implementation of `baseForOwn` which iterates over `object`
 * properties returned by `keysFunc` and invokes `iteratee` for each property.
 * Iteratee functions may exit iteration early by explicitly returning `false`.
 *
 * @private
 * @param {Object} object The object to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @param {Function} keysFunc The function to get the keys of `object`.
 * @returns {Object} Returns `object`.
 */
//baseForOwn的基础实现,通过keysFunc遍历对象每一个属性,用iteratee处理每个属性值
//iteratee可以返回false跳出循环
function baseFor(object, iteratee, keysFunc) {
  const iterable = Object(object)
  const props = keysFunc(object)//key组成的数组
  let { length } = props//key数组长度
  let index = -1//循环索引

  while (length--) {//遍历key数组
    const key = props[++index]//当前key
    if (iteratee(iterable[key], key, iterable) === false) {//调用iteratee
      break
    }
  }
  return object
}

export default baseFor
keys
import arrayLikeKeys from './.internal/arrayLikeKeys.js'
import isArrayLike from './isArrayLike.js'

/**
 * Creates an array of the own enumerable property names of `object`.
 *
 * **Note:** Non-object values are coerced to objects. See the
 * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
 * for more details.
 *
 * @since 0.1.0
 * @category Object
 * @param {Object} object The object to query.
 * @returns {Array} Returns the array of property names.
 * @see values, valuesIn
 * @example
 *
 * function Foo() {
 *   this.a = 1
 *   this.b = 2
 * }
 *
 * Foo.prototype.c = 3
 *
 * keys(new Foo)
 * // => ['a', 'b'] (iteration order is not guaranteed)
 *
 * keys('hi')
 * // => ['0', '1']
 */
//创建一个给定对象的自身可枚举属性组成的数组
function keys(object) {
  return isArrayLike(object)
    ? arrayLikeKeys(object)
    : Object.keys(Object(object))
    //如果object是array-like对象,就调用arrayLikeKeys,否则使用原生的Object.keys方法
}

export default keys

 

posted @ 2018-11-06 10:33  hahazexia  阅读(239)  评论(0)    收藏  举报