Lodash _.differenceBy() method is used to remove the values from the original array by iterating over each element in the array by using the Iterate function. It is almost same as _.difference() function.
Syntax:
lodash.differenceBy(array, [values], [iterate=_.identity])
mentionedParameters:
This function accepts three parameters as mentioned above and described below
- array: This is the array from which the values are to be removed.
- values: It is the Array of values that is to be removed from the original array.
- Iterate: This is the function that iterate over each element.
Note: If the iterate function is not given then _.differenceBy() function act as _.difference() function.
Return Value:
This function returns an array.
Example 1: In this example, we use the lodash library to subtract one array (val
) from another (array1
).
Javascript
const _ = require( "lodash" );
let array1 = [1, 2, 3, 4.2]
let val = [2, 3, 3, 5]
let newArray = _.differenceBy(
array1, val, Math.double);
console.log( "Before : " , array1);
console.log( "After : " , newArray);
|
Output:

Example 2: In this example, we use lodash library to find differences between arrays, with and without a custom comparator function.
Javascript
const _ = require( "lodash" );
let array1 = [1, 2, 3, 4.2]
let val = [2, 3, 4, 5]
let newArray1 = _.differenceBy(
array1, val, Math.floor);
let newArray2 = _.differenceBy(array1, val);
console.log( "Before : " , array1);
console.log( "When compare funct is given: " ,
newArray1);
console.log( "When compare funct is not given: " ,
newArray2);
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Nov, 2023
Like Article
Save Article