Lodash - IntersectionBy 方法

语法

_.intersectionBy([arrays], [iteratee=_.identity])

此方法与 _.intersection 类似,不同之处在于它接受 iteratee,它会针对每个数组的每个元素调用 iteratee 来生成比较它们的标准。结果值的顺序和引用由第一个数组决定。iteratee 使用一个参数调用:(value)。

参数

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

  • [iteratee=_.identity] (Function) −每个元素调用的迭代器。

输出

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

示例

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

listOfNumbers = _.intersectionBy(numbers, [1.3, 2.2], Math.floor);
console.log(listOfNumbers);

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

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

命令

\>node tester.js

输出

[ 1.7, 2.4 ]
[ { x: 4 } ]

lodash_array.html