What is the _.difference() method in Lodash?
Overview
The _.difference() method in Lodash creates a new array from existing arrays by excluding all the common values.
Syntax
_.difference(array, [values])
Parameters
This method accepts the following parameters:
array: This is the array to be inspected.[values]: These are the values to be excluded.
Return value
This method returns an array with all the common values excluded.
Example
Let’s look at an example of the _.difference() method in the code snippet below:
Explanation
In the HTML tab:
- Line 5: We import the
lodashscript.
In the JavaScript tab:
- Line 2: We create an array and initialize it with a few values.
- Line 5: We create another array and initialize it with a few values.
- Line 8: We use the
_.difference()method to exclude the common values in both arrays. - Line 11: We print the output on the console.
Output
We see an array in which all the common values are excluded in the output.
Note: Only the non-common values of the first array are included in the result.