Lodash _.pullAllBy() 方法
_.pullAllBy()方法用于通过使用 Iteratee函数迭代数组中的每个元素来从原始数组中删除值。它与 _.pullAll()函数几乎相同。
句法:
_.pullAllBy(array, values, [iteratee=_.identity])
参数:此方法接受上面提到的两个参数,如下所述:
- array:该参数保存需要修改的数组。
- values:此参数保存需要从第一个数组中删除的数组中的值。
- Iteratee:这是迭代每个元素的函数。
返回值:它返回一个数组。
注意:如果没有给出 iteratee函数,则 _.pullAllBy()函数充当 _.pullAll()函数。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [1, 2, 3, 4.2]
// Array to be subtracted
let val = [2, 3, 3, 5]
// Printing the original array
console.log("Before : ", array1);
// Array after _.pullAllBy()
// method where Math.double is the
// comparable function
_.pullAllBy(array1, val, Math.double);
// Printing the output
console.log("After : ", array1);
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [1, 2, 3, 4.2]
let array2 = [1, 2, 3, 4.2]
// Value array to be subtracted
let val = [2, 3, 4, 5]
// Printing the original array
console.log("Before : ", array1);
// Array after _.pullAllBy()
// method where Math.double is the
// comparable function
_.pullAllBy(
array1, val, Math.floor);
// Array after _.pullAllBy function
// where no comparable function is given
_.pullAllBy(array2, val);
// Printing the output
console.log("When compare funct is given: ",
array1);
// Printing the output
console.log("When compare funct is not given: ",
array2);
输出:
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [1, 2, 3, 4.2]
let array2 = [1, 2, 3, 4.2]
// Value array to be subtracted
let val = [2, 3, 4, 5]
// Printing the original array
console.log("Before : ", array1);
// Array after _.pullAllBy()
// method where Math.double is the
// comparable function
_.pullAllBy(
array1, val, Math.floor);
// Array after _.pullAllBy function
// where no comparable function is given
_.pullAllBy(array2, val);
// Printing the output
console.log("When compare funct is given: ",
array1);
// Printing the output
console.log("When compare funct is not given: ",
array2);
输出:
注意:这在普通 JavaScript 中不起作用,因为它需要安装库 lodash。