To use the difference
function from lodash in JavaScript, first, you need to install the lodash library using npm:
26 chars2 lines
Then, you can import the difference
function in your JavaScript file:
166 chars9 lines
The difference
function takes multiple arrays as arguments and returns a new array with the values that are unique to the first array, i.e., the elements that are not present in any of the other arrays. In the example above, the result
array contains the elements [1, 4]
, which are the values from array1
that are not present in array2
.
You can also use the difference
function with more than two arrays:
170 chars8 lines
In the example above, the result
array contains only the element 1
, which is the only element that is not present in any of the other arrays.
gistlibby LogSnag