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.htm
廣告