_.pullAllWith(array, values, [comparator])

31

_.pullAllWith(array, values, [comparator])

_.pullAllWith和pullAll类似,却别是要传递一个自定义的比较器来比较元素是否相等

参数

array (Array): 需要修改的数组
values (Array): 需要删除的元素的数组
[comparator] (Function): 比较元素是否相等的比较器

返回值

(Array): 返回删除元素后的原数组

例子

var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
 
_.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
console.log(array);
// => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]

源代码:

源代码大体和pullAll一样,细节省略

import basePullAll from './.internal/basePullAll.js'

/**
 * This method is like `pullAll` except that it accepts `comparator` which
 * is invoked to compare elements of `array` to `values`. The comparator is
 * invoked with two arguments: (arrVal, othVal).
 *
 * **Note:** Unlike `differenceWith`, this method mutates `array`.
 *
 * @since 4.6.0
 * @category Array
 * @param {Array} array The array to modify.
 * @param {Array} values The values to remove.
 * @param {Function} [comparator] The comparator invoked per element.
 * @returns {Array} Returns `array`.
 * @see pull, pullAll, pullAllBy, pullAt, remove, reject
 * @example
 *
 * const array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }]
 *
 * pullAllWith(array, [{ 'x': 3, 'y': 4 }], isEqual)
 * console.log(array)
 * // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]
 */
//和pullAll类似,却别是要传递一个自定义的比较器来比较元素是否相等
function pullAllWith(array, values, comparator) {
  return (array != null && array.length && values != null && values.length)
    ? basePullAll(array, values, undefined, comparator)
    : array
    //如果array不是null,并且array有元素,并且values不为null,并且values有元素,就调用basePullAll
    //否则返回原数组
}

export default pullAllWith

 

posted @ 2018-10-16 10:37  hahazexia  阅读(360)  评论(0)    收藏  举报