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

76

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

_.forEachRight类似forEach方法,区别是遍历的方向是从右向左

参数

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

返回值

(*): 返回原集合

例子

_.forEachRight([1, 2], function(value) {
  console.log(value);
});
// => Logs `2` then `1`.

源代码:

import arrayEachRight from './.internal/arrayEachRight.js'
import baseEachRight from './.internal/baseEachRight.js'

/**
 * This method is like `forEach` except that it iterates over elements of
 * `collection` from right to left.
 *
 * @since 2.0.0
 * @alias eachRight
 * @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 forEach, forIn, forInRight, forOwn, forOwnRight
 * @example
 *
 * forEachRight([1, 2], value => console.log(value))
 * // => Logs `2` then `1`.
 */
//类似forEach方法,区别是遍历的方向是从右向左
function forEachRight(collection, iteratee) {
  const func = Array.isArray(collection) ? arrayEachRight : baseEachRight//如果是数组,就调用arrayEachRight,否则baseEachRight
  return func(collection, iteratee)
}

export default forEachRight
arrayEachRight
/**
 * A specialized version of `forEachRight` for arrays.
 *
 * @private
 * @param {Array} [array] The array to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array} Returns `array`.
 */
//针对数组的forEachRight方法
function arrayEachRight(array, iteratee) {
  let length = array == null ? 0 : array.length//数组长度

  while (length--) {//循环数组,如果iteratee返回false,就提前跳出循环
    if (iteratee(array[length], length, array) === false) {
      break
    }
  }
  return array
}

export default arrayEachRight
baseEachRight
import baseForOwnRight from './baseForOwnRight.js'
import isArrayLike from '../isArrayLike.js'

/**
 * The base implementation of `forEachRight`.
 *
 * @private
 * @param {Array|Object} collection The collection to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array|Object} Returns `collection`.
 */
//forEachRight的基础实现
function baseEachRight(collection, iteratee) {
  if (collection == null) {//如果collection为空,就直接返回
    return collection
  }
  if (!isArrayLike(collection)) {//如果collection不是array-like对象,调用baseForOwnRight处理
    return baseForOwnRight(collection, iteratee)
  }
  const iterable = Object(collection)//array-like对象化
  let length = collection.length//循环索引,从右往左

  while (length--) {//循环,如果iteratee返回了false,就提前跳出循环
    if (iteratee(iterable[length], length, iterable) === false) {
      break
    }
  }
  return collection
}

export default baseEachRight
baseForOwnRight
import baseForRight from './baseForRight.js'
import keys from '../keys.js'

/**
 * The base implementation of `forOwnRight`.
 *
 * @private
 * @param {Object} object The object to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Object} Returns `object`.
 */
//forOwnRight基础实现
function baseForOwnRight(object, iteratee) {
  return object && baseForRight(object, iteratee, keys)//如果object存在调用baseForRight处理
}

export default baseForOwnRight
baseForRight
/**
 * This function is like `baseFor` except that it iterates over properties
 * in the opposite order.
 *
 * @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`.
 */
//类似baseFor方法,遍历属性从相反的顺序
function baseForRight(object, iteratee, keysFunc) {
  const iterable = Object(object)
  const props = keysFunc(object)//键组成的数组
  let { length } = props//键数组长度

  while (length--) {//循环键数组,从右往左的顺序
    const key = props[length]//当前键
    if (iteratee(iterable[key], key, iterable) === false) {//调用iteratee,如果返回false,就提前跳出循环
      break
    }
  }
  return object
}

export default baseForRight
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:46  hahazexia  阅读(360)  评论(0)    收藏  举报