_.reject(collection, [predicate=_.identity])

86

_.reject(collection, [predicate=_.identity])
_.reject与filter方法相对立,返回collection中调用predicate不返回true的元素组成的数组

参数

collection (Array|Object): 需要遍历的集合
[predicate=_.identity] (Function): 每一次遍历调用的方法

返回值

(Array): 返回过滤后的元素组成的新数组

例子

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

源代码:

省略iteratee的处理,想看iteratee的处理看这里,https://www.cnblogs.com/hahazexia/p/9901435.html

import filter from './filter.js'
import filterObject from './filterObject.js'
import negate from './negate.js'

/**
 * The opposite of `filter` this method returns the elements of `collection`
 * that `predicate` does **not** return truthy for.
 *
 * @since 0.1.0
 * @category Collection
 * @param {Array|Object} collection The collection to iterate over.
 * @param {Function} predicate The function invoked per iteration.
 * @returns {Array} Returns the new filtered array.
 * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, filter
 * @example
 *
 * const users = [
 *   { 'user': 'barney', 'active': true },
 *   { 'user': 'fred',   'active': false }
 * ]
 *
 * reject(users, ({ active }) => active)
 * // => objects for ['fred']
 */
//与filter方法相对立,返回collection中调用predicate不返回true的元素组成的数组
function reject(collection, predicate) {
  const func = Array.isArray(collection) ? filter : filterObject
  //如果collection是数组,就调用filter处理,否则filterObject
  return func(collection, negate(predicate))
  //用negate处理predicate,是它的返回值取反
}

export default reject
filterObject
/**
 * Iterates over properties of `object`, returning an array of all elements
 * `predicate` returns truthy for. The predicate is invoked with three
 * arguments: (value, key, object).
 *
 * @since 5.0.0
 * @category Object
 * @param {Object} object The object to iterate over.
 * @param {Function} predicate The function invoked per iteration.
 * @returns {Array} Returns the new filtered array.
 * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject
 * @example
 *
 * const object = { 'a': 5, 'b': 8, 'c': 10 }
 *
 * filterObject(object, (n) => !(n % 5))
 * // => [5, 10]
 */
//遍历object的属性,返回调用predicate返回值为true的元素组成的数组
function filterObject(object, predicate) {
  object = Object(object)
  const result = []//结果数组

  Object.keys(object).forEach((key) => {//遍历object的键数组
    const value = object[key]//当前object的key对应的value
    if (predicate(value, key, object)) {//如果predicate调用后返回true,就将当前元素出入结果数组中
      result.push(value)
    }
  })
  return result
}

export default filterObject
negate
/**
 * Creates a function that negates the result of the predicate `func`. The
 * `func` predicate is invoked with the `this` binding and arguments of the
 * created function.
 *
 * @since 3.0.0
 * @category Function
 * @param {Function} predicate The predicate to negate.
 * @returns {Function} Returns the new negated function.
 * @example
 *
 * function isEven(n) {
 *   return n % 2 == 0
 * }
 *
 * filter([1, 2, 3, 4, 5, 6], negate(isEven))
 * // => [1, 3, 5]
 */
//将predicate方法返回的结果取反
function negate(predicate) {
  if (typeof predicate != 'function') {//如果predicate不是function,抛出错误
    throw new TypeError('Expected a function')
  }
  return function(...args) {//返回一个方法,这个方法将predicate的返回值取反
    return !predicate.apply(this, args)
  }
}

export default negate
filter
/**
 * Iterates over elements of `array`, returning an array of all elements
 * `predicate` returns truthy for. The predicate is invoked with three
 * arguments: (value, index, array).
 *
 * **Note:** Unlike `remove`, this method returns a new array.
 *
 * @since 5.0.0
 * @category Array
 * @param {Array} array The array to iterate over.
 * @param {Function} predicate The function invoked per iteration.
 * @returns {Array} Returns the new filtered array.
 * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject
 * @example
 *
 * const users = [
 *   { 'user': 'barney', 'active': true },
 *   { 'user': 'fred',   'active': false }
 * ]
 *
 * filter(users, ({ active }) => active)
 * // => objects for ['barney']
 */
//和原生的filter方法类似,遍历数组,将predicate处理后结果为真值的元素组成一个新数组返回
function filter(array, predicate) {
  let index = -1//循环索引
  let resIndex = 0
  const length = array == null ? 0 : array.length//数组长度
  const result = []//结果数组

  while (++index < length) {
    const value = array[index]
    if (predicate(value, index, array)) {//如果predicate返回true,就把当前元素加入结果数组
      result[resIndex++] = value
    }
  }
  return result
}

export default filter

 

posted @ 2018-11-09 13:38  hahazexia  阅读(656)  评论(0)    收藏  举报