_.differenceBy()方法用於通過使用Iteratee函數在數組中的每個元素上進行迭代來從原始數組中刪除值。與_.difference()函數幾乎相同。
用法:
lodash.differenceBy(array, [values], [iteratee=_.identity])
參數:此函數接受上麵提到的三個參數,並在下麵進行描述
- array:這是要從中刪除值的數組。
- values:從原始數組中刪除的是值數組。
- Iteratee:這是對每個元素進行迭代的函數。
注意:如果未提供iteratee函數,則_.differenceBy()函數充當_.difference()函數。
返回值:該函數返回一個數組。
範例1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Ooriginal array
let array1 = [1, 2, 3, 4.2]
// Array to be subtracted
let val = [2, 3, 3, 5]
// New Array after _.differenceBy()
// method where Math.double is the
// comparable function
let newArray = _.differenceBy(
array1, val, Math.double);
// Printing the original array
console.log("Before:", array1);
// Printing the output
console.log("After:", newArray);
輸出:
範例2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [1, 2, 3, 4.2]
// Value array to be subtracted
let val = [2, 3, 4, 5]
// new Array after _.differenceBy()
// method where Math.double is the
// comparable function
let newArray1 = _.differenceBy(
array1, val, Math.floor);
// New Array after _.differenceBy function
// where no comparable function is given
let newArray2 = _.differenceBy(array1, val);
// Printing the original array
console.log("Before:", array1);
// Nrinting the output
console.log("When compare funct is given:",
newArray1);
// Printing the output
console.log("When compare funct is not given:",
newArray2);
輸出:
相關用法
- 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 _.differenceBy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。