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

66

_.forEach(collection, [iteratee=_.identity])
_.forEach类似原生forEach,遍历每一个元素,对象用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

 

posted @ 2018-10-28 11:35  hahazexia  阅读(760)  评论(0)    收藏  举报