主题
differenceWith
js
_.differenceWith(array, [values], [comparator])
此方法与 _.difference
类似,不同之处在于它接受 comparator
,调用该方法可比较 array
和 values
的元素。结果值的顺序和引用由第一个数组决定。比较器使用两个参数调用:(arrVal, othVal)。
¥This method is like _.difference
except that it accepts comparator
which is invoked to compare elements of array
to values
. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arrVal, othVal).
注意:与 _.pullAllWith
不同,此方法返回一个新数组。
¥Note: Unlike _.pullAllWith
, this method returns a new array.
新增于
¥Since
4.0.0
参数
¥Arguments
array
(数组):要检查的数组。¥
array
(Array): The array to inspect.[values]
(...数组):要排除的值。¥
[values]
(...Array): The values to exclude.[comparator]
(函数):每个元素调用的比较器。¥
[comparator]
(Function): The comparator invoked per element.
返回
¥Returns
(数组):返回新的过滤值数组。
¥(Array): Returns the new array of filtered values.
示例
¥Example
js
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]