Skip to content

unionWith

js
_.unionWith([arrays], [comparator])

此方法与 _.union 类似,不同之处在于它接受 comparator,调用该方法可比较 arrays 的元素。结果值从出现该值的第一个数组中选择。比较器使用两个参数调用:(arrVal, othVal)。

¥This method is like _.union except that it accepts comparator which is invoked to compare elements of arrays. Result values are chosen from the first array in which the value occurs. The comparator is invoked with two arguments: (arrVal, othVal).

新增于

¥Since

4.0.0

参数

¥Arguments

  1. [arrays] (...数组):要检查的数组。

    ¥[arrays] (...Array): The arrays to inspect.

  2. [comparator] (函数):每个元素调用的比较器。

    ¥[comparator] (Function): The comparator invoked per element.

返回

¥Returns

(数组):返回新的组合值数组。

¥(Array): Returns the new array of combined values.

示例

¥Example

js
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];

_.unionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

Lodash v4.17 中文网 - 粤ICP备13048890号