📜  Lodash _.intersectionBy() 方法

📅  最后修改于: 2022-05-13 01:56:37.067000             🧑  作者: Mango

Lodash _.intersectionBy() 方法

Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
_.intersectionBy()用于根据迭代数组的每个元素的某个函数将 a 数组与任意数量的数组相交。它在对数组进行交集后返回数组。

句法:

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

参数:

  • 数组:它是必须取交集的数组。
  • iteratee=_.identity:它是迭代数组的每个元素的函数,其交集将被取走。

返回值:返回相交后的数组,不重复。

注意:在使用下面给出的代码之前,请通过npm install lodash安装 lodash 模块。

示例 1:当没有给出 Iteratee函数时,它的行为与 lodash 的 intersection()函数相同。

javascript
// Requiring the lodash library
const _ = require("lodash");
  
// Original arrays
let array1 = [1, 2, 4, 3, 4, 4]
let array2 = [1, 2, 5, 6]
  
// Using _.intersectionBy() method
let newArray = _.intersectionBy(array1, array2);
  
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
  
// Printing the newArray after intersection
console.log("new Array: ", newArray)


javascript
// Requiring the lodash library
const _ = require("lodash");
  
// Original array and array1 
// float values are given
let array1 = [1.1, 2.6, 4, 3.2, 1, 2]
let array2 = [1, 2, 3, 5, 6]
  
// Using _.intersection() method
// when this function is run array1
// looks like array1=[2, 3, 4, 4, 1, 2]
// after that intersection is taken
let newArray = lodash.intersectionBy(
        array1, array2, Math.ceil);
  
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
  
// Printing the newArray
console.log("new Array: ", newArray)


javascript
// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = ["a", "b", "c", "a"]
let array2 = ["a", "d", "e", "a"]
  
// Using _.intersection() method
let newArray = lodash.intersectionBy(
        array1, array2, "a");
  
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
  
// Printing the newArray
console.log("new Array: ", newArray)


输出:

示例 2:当使用 Math.ceil()函数运行 array1 并且 array1 的所有值与下一个最接近的较大整数进行比较时,然后取交集。

javascript

// Requiring the lodash library
const _ = require("lodash");
  
// Original array and array1 
// float values are given
let array1 = [1.1, 2.6, 4, 3.2, 1, 2]
let array2 = [1, 2, 3, 5, 6]
  
// Using _.intersection() method
// when this function is run array1
// looks like array1=[2, 3, 4, 4, 1, 2]
// after that intersection is taken
let newArray = lodash.intersectionBy(
        array1, array2, Math.ceil);
  
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
  
// Printing the newArray
console.log("new Array: ", newArray)

输出:

示例 3:当存在多个公共元素时,它只返回一次,并且在数组中不返回重复项。

javascript

// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = ["a", "b", "c", "a"]
let array2 = ["a", "d", "e", "a"]
  
// Using _.intersection() method
let newArray = lodash.intersectionBy(
        array1, array2, "a");
  
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
  
// Printing the newArray
console.log("new Array: ", newArray)

输出: