Lodash - unionWith 方法

语法

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

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

参数

  • [arrays] (...Array) − 要检查的数组。

  • [comparator] (函数) −每个元素调用的比较器。

输出

  • (Array) − 返回组合值的新数组。

示例

var _ = require('lodash');
var objects = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
var others = [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
var result = _.unionWith(objects, others, _.isEqual);
console.log(result);

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

[ { x: 1, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 1 } ]

lodash_array.html