_.takeRightWhile(array, [predicate=_.identity])

47

_.takeRightWhile(array, [predicate=_.identity])

_takeRightWhile从数组结尾获取元素切片,获取元素直到predicate返回false。predicate接受三个参数:value index array

参数

array (Array): 要取切片的数组
[predicate=_.identity] (Function): 遍历时调用的函数

返回值

(Array): 返回数组的切片

例子

var users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': false }
];
 
_.takeRightWhile(users, function(o) { return !o.active; });
// => objects for ['fred', 'pebbles']
 
// The `_.matches` iteratee shorthand.
_.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['pebbles']
 
// The `_.matchesProperty` iteratee shorthand.
_.takeRightWhile(users, ['active', false]);
// => objects for ['fred', 'pebbles']
 
// The `_.property` iteratee shorthand.
_.takeRightWhile(users, 'active');
// => []

源代码:

最终生成的代码中predicate参数可以处理对象,对象的某个属性的特点值,和某个直接属性值,和dropRightWhile一样,这里细节省略。

import baseWhile from './.internal/baseWhile.js'

/**
 * Creates a slice of `array` with elements taken from the end. Elements are
 * taken until `predicate` returns falsey. The predicate is invoked with
 * three arguments: (value, index, array).
 *
 * @since 3.0.0
 * @category Array
 * @param {Array} array The array to query.
 * @param {Function} predicate The function invoked per iteration.
 * @returns {Array} Returns the slice of `array`.
 * @example
 *
 * const users = [
 *   { 'user': 'barney',  'active': false },
 *   { 'user': 'fred',    'active': true },
 *   { 'user': 'pebbles', 'active': true }
 * ]
 *
 * takeRightWhile(users, ({ active }) => active)
 * // => objects for ['fred', 'pebbles']
 */
//从数组结尾获取元素切片,获取元素直到predicate返回false。predicate接受三个参数:value index array
function takeRightWhile(array, predicate) {
  return (array != null && array.length)
    ? baseWhile(array, predicate, false, true)
    : []
    //如果数组为空或者数组没有长度,返回空数组
    //否则调用baseWhile
}

export default takeRightWhile

baseWhile

import slice from '../slice.js'

/**
 * The base implementation of methods like `dropWhile` and `takeWhile`.
 *
 * @private
 * @param {Array} array The array to query.
 * @param {Function} predicate The function invoked per iteration.
 * @param {boolean} [isDrop] Specify dropping elements instead of taking them.
 * @param {boolean} [fromRight] Specify iterating from right to left.
 * @returns {Array} Returns the slice of `array`.
 */
//dropWhile和takeWhile的基础实现
//array需要处理的数组,predicate遍历器函数,isDrop指定丢弃或者获取元素,fromRight指定循环的方向
function baseWhile(array, predicate, isDrop, fromRight) {
  const { length } = array//数组的长度
  let index = fromRight ? length : -1//循环索引,如果fromRight为true,从右往左循环,起始索引是数组长度;如果fromRight为false,从左往右循环,起始索引是-1

  while ((fromRight ? index-- : ++index < length) &&
    predicate(array[index], index, array)) {}
    //循环数组,length--从右往左循环,-1++从左往右循环
    //为数组循环值调用predicate,直到predicate返回false,循环索引index后面slice的时候有用

  return isDrop
    ? slice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
    : slice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index))
    //isDrop为true的时候,如果从右往左丢弃,起始slice为0,结束slice为index+1;如果从左往右丢弃,起始slice为index,结束slice为length
    //isDrop为false的时候,如果从右往左获取,起始slice为index+1,结束slice为length;如果从左往右获取,起始slice为0,结束slice为index
}

export default baseWhile

slice

/**
 * Creates a slice of `array` from `start` up to, but not including, `end`.
 *
 * **Note:** This method is used instead of
 * [`Array#slice`](https://mdn.io/Array/slice) to ensure dense arrays are
 * returned.
 *
 * @since 3.0.0
 * @category Array
 * @param {Array} array The array to slice.
 * @param {number} [start=0] The start position. A negative index will be treated as an offset from the end.
 * @param {number} [end=array.length] The end position. A negative index will be treated as an offset from the end.
 * @returns {Array} Returns the slice of `array`.
 * @example
 *
 * var array = [1, 2, 3, 4]
 *
 * _.slice(array, 2)
 * // => [3, 4]
 */
//创建一个数组array的切片,从起始索引到结束索引,不包括结束索引
function slice(array, start, end) {//array要操作的数组,start起始索引,end结束索引
  let length = array == null ? 0 : array.length//数组长度
  if (!length) {//如果数组长度为0,就返回空数组
    return []
  }
  start = start == null ? 0 : start//start起始索引,默认值是0
  end = end === undefined ? length : end//end结束索引,默认是数组长度

  if (start < 0) {//起始索引是负数处理,是负数就相当于从末尾往开头数,也就是和lengh相加
    start = -start > length ? 0 : (length + start)//和length相加后如果还是小于0就等于0
  }
  end = end > length ? length : end//结束索引如果大于length就让它等于length
  if (end < 0) {//处理结束索引是负数
    end += length
  }
  length = start > end ? 0 : ((end - start) >>> 0)//根据start和end计算这个切片的长度,如果起始在结束后面那么切片长度为0,否则相减并且取整
  start >>>= 0//start取整

  let index = -1//循环索引
  const result = new Array(length)//创建切片长度的数组作为结果数组
  while (++index < length) {//循环切片长度的次数,给结果数组每一项赋值
    result[index] = array[index + start]
  }
  return result
}

export default slice

 

posted @ 2018-10-20 17:33  hahazexia  阅读(166)  评论(0)    收藏  举报