Lodash是一个JavaScript库,可在Underscore.js的顶部使用。 Lodash帮助处理数组,字符串,对象,数字等。
_.intersectionwith()方法用于获取一个或多个数组的交集。它与lodash中的交集函数相同,唯一的区别是它接受一个比较器,该比较器被调用以比较数组的元素。
用法:
intersectionWith([arrays], [comparator])
参数:此方法接受上面提到的两个参数,并在下面进行描述:
- arrays:它以数组作为参数。
 - comparator:该函数可对数组的每个值进行迭代,并将这些值与给定的比较器函数进行比较。
 
返回值:数组交集后返回数组。
注意:在使用下面给出的代码之前,请使用命令npm install lodash安装lodash模块。
范例1:
Javascript
// Requiring the lodash library 
const _ = require("lodash"); 
  
// Original array 
let array1 = [ 
    { "a":1 }, { "b":2 }, 
    { "b":2, "a":1 } 
] 
  
let array2 = [ 
    { "a":1, "b":2 }, 
    { "a":1 } 
] 
  
// Using _.intersectionWith() method 
let newArray = _.intersectionWith( 
    array1, array2, _.isEqual); 
  
// Printing original Array 
console.log("original Array1:", array1) 
console.log("original Array2:", array2) 
  
// Printing the newArray 
console.log("new Array:", newArray)输出:

范例2:当不使用比较器函数即_.isequal()时,输出为空数组。
Javascript
// Requiring the lodash library 
const _ = require("lodash"); 
  
// Original array 
let array1 = [ 
    { "a":1 }, { "b":2 }, 
    { "b":2, "a":1 } 
] 
  
let array2 = [ 
    { "a":1, "b":2 }, 
    { "a":1 } 
] 
  
// Using _.intersectionWith() method 
// and no comparator function 
let newArray = _.intersectionWith( 
    array1, array2); 
  
// Printing original Array 
console.log("original Array1:", array1) 
console.log("original Array2:", array2) 
  
// Printing the newArray 
console.log("new Array:", newArray)输出:

相关用法
- Lodash _.method()用法及代码示例
 - Lodash _.sneq()用法及代码示例
 - Lodash _.toQuery()用法及代码示例
 - Lodash _.uniqWith()用法及代码示例
 - Lodash _.xorWith()用法及代码示例
 - Lodash _.head()用法及代码示例
 - Lodash _.remove()用法及代码示例
 - Lodash _.pullAt()用法及代码示例
 - Lodash _.pullAll()用法及代码示例
 - Lodash _.pull()用法及代码示例
 - Lodash _.nth()用法及代码示例
 - Lodash _.takeRight()用法及代码示例
 - Lodash _.take()用法及代码示例
 - Lodash _.sortedLastIndex()用法及代码示例
 - Lodash _.fromPairs()用法及代码示例
 - Lodash _.differenceWith()用法及代码示例
 - Lodash _.castArray()用法及代码示例
 - Lodash _.cloneDeep()用法及代码示例
 - Lodash _.clone()用法及代码示例
 - Lodash _.sampleSize()用法及代码示例
 - Lodash _.find()用法及代码示例
 - Lodash _.zipWith()用法及代码示例
 - Lodash _.zipObject()用法及代码示例
 - Lodash _.xor()用法及代码示例
 
注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 Lodash _.intersectionWith() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
