javascript

Get all values from an array which does not exist in other array Lodash

To get all values from an array which does not exist in the other array, you can use lodash method _.difference(). It will return the array which contains values that does not match with second array.

const arr1 = [2, 1, 5, 6];
const arr2 = [2, 3, 10, 1];

//GET VALUES FROM arr1 that does not exist in arr2
const diff_arr = _.difference(arr1, arr2);
Output
[5,6]

Live Demo :

In the code snippet, we are matching arr1 with arr2 and getting all values from arr1 in an array which does not exist in arr2. For this we are using lodash method _.difference. The syntax is for this is as below:

_.difference(Array, [Values to Match])

In the first paramter as an array we are passing our array whose values needs to be checked with second parameter array.

Was this helpful?