Syntax, Parameter, and Return Value
Syntax:
_.differenceWith(array, [values], [comparator])
Parameters:
-
array (Array): The array to inspect.
-
values (Array): The values to exclude.
- comparator (Function): The comparator invoked per element.
Return Value:
Returns a new array of filtered values.
In this method, the comparator function plays a critical role. It is called with two arguments: (value, other). For each element in the array, the comparator is invoked with the element and each element of [values] array. If the comparator returns false for all comparisons, the element is included in the result array.
Examples
Example 1: Basic Usage
JavaScript
const _ = require('lodash');
const array = [1, 2, 3];
const values = [2, 3];
const result = _.differenceWith(array, values, _.isEqual);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1]
In this example, _.isEqual is used as the comparator to perform a deep comparison between elements.
Example 2: Custom Comparator Function
JavaScript
const array = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
const values = [{ 'x': 1, 'y': 2 }];
const result = _.differenceWith(array, values, _.isEqual);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'x': 2, 'y': 1 }]
Here, a custom comparator (in this case, _.isEqual) is used to compare objects.
Example 3: Using Arrow Function as Comparator
JavaScript
const array = [1.2, 2.3, 3.4];
const values = [2.3];
const result = _.differenceWith(array, values, (a, b) => Math.floor(a) === Math.floor(b));
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1.2, 3.4]
This example demonstrates a comparator that compares the floored values of numbers.
Example 4: Complex Data Structures
JavaScript
const users = [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }];
const exclude = [{ 'user': 'barney', 'age': 36 }];
const result = _.differenceWith(users, exclude, _.isEqual);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'fred', 'age': 40 }]
This shows _.differenceWith being used to filter out objects in a more complex array.
Frequently Asked Questions
How does _.differenceWith() differ from _.difference()?
_.differenceWith() allows for a custom comparator function for more complex comparisons, while _.difference() only compares based on strict equality (===).
Can _.differenceWith() be used with arrays of objects?
Yes, it is particularly useful for arrays of objects, as the comparator function can be tailored to compare object properties.
Is _.differenceWith() chainable in Lodash's chain syntax?
Yes, like most Lodash methods, it can be used within Lodash's chain syntax for more fluent and readable code.
Conclusion
The _.differenceWith() method in Lodash offers a flexible and powerful way to filter array elements based on custom logic. Its ability to utilize a comparator function makes it highly suitable for scenarios requiring detailed comparison criteria, especially with complex data types like objects. By understanding and implementing this method, developers can efficiently handle array comparisons and manipulations, making it a valuable tool in JavaScript programming.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.