I have 2 arrays that they are identical at first but the user may remove (not able to add but just remove) items from the second array. I want to find items that are in the first array but not in the second one.
I can think of several ways to do this, but as these arrays can be very large, I'm curious to find out if anyone can offer a more efficient way:
$.grep( firstArray, function( n, i ){
return $.inArray(n, secondArray) == -1;
});
You could try to make use of filter
and indexOf
array methods as below:
var firstArray = [1,2,3,4,5,6];
var secondArray = [3,4,6];
var result = firstArray.filter(item => secondArray.indexOf(item) == -1);
console.log(result);
Assuming the arrays have the same order, then you could filter with an index as closure.
var array1 = [1, 2, 3, 4, 5, 6, 7, 1, 2, 3],
array2 = [2, 4, 6, 7, 2],
missing = array1.filter((i => a => a !== array2[i] || !++i)(0));
console.log(missing);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With