Lodash是一個在underscore.js頂部運行的JavaScript庫。 Lodash幫助處理數組,字符串,對象,數字等。
_.intersectionBy()用於基於對數組的每個元素進行迭代的某些函數來獲取數組與任意數量的數組的交集。數組交集後返回數組。
用法:
_.intersectionBy([arrays], [iteratee=_.identity])
參數:
- arrays:它是必須采用交集的數組。
- iteratee = _。identity:它是對要采用交集的數組的每個元素進行迭代的函數。
返回值:交集後返回無重複的數組。
注意:請通過安裝lodash模塊npm install 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)
輸出:
範例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)
輸出:
相關用法
- Lodash _.xor()用法及代碼示例
- Lodash _.take()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.last()用法及代碼示例
- Lodash _.head()用法及代碼示例
- Lodash _.remove()用法及代碼示例
- Lodash _.isBoolean()用法及代碼示例
- Lodash _.chunk()用法及代碼示例
- Lodash _.isArrayLikeObject()用法及代碼示例
- Lodash _.pullAll()用法及代碼示例
- Lodash _.fill()用法及代碼示例
- Lodash _.pullAt()用法及代碼示例
- Lodash _.flatten()用法及代碼示例
- Lodash _.zipObject()用法及代碼示例
- Lodash _.isBuffer()用法及代碼示例
- Lodash _.isDate()用法及代碼示例
- Lodash _.isEmpty()用法及代碼示例
- Lodash _.indexOf()用法及代碼示例
- Lodash _.intersectionWith()用法及代碼示例
- Lodash _.zipWith()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 Lodash _.intersectionBy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。