Lodash - IntersectionWith 方法

语法

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

此方法与 _.intersection 类似,不同之处在于它接受调用来比较数组元素的比较器。结果值的顺序和引用由第一个数组决定。比较器使用两个参数调用:(arrVal, othVal)。

参数

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

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

输出

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

示例

var _ = require('lodash');
var numbers = [1.7, 2.4, 3.6, 4.2];
var listOfNumbers = '';

listOfNumbers = _.intersectionWith([{ 'x': 4 }, { 'x': 1 }], [{ 'x': 4 }], _.isEqual);
console.log(listOfNumbers);

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

命令

\>node tester.js

输出

[ { x: 4 } ]

lodash_array.html