I have 2 API requests that return 2 arrays(1 Array for each API request as result) for which have 1 common value i.e. timestamp.
I am looking to compare both the arrays and find which Array items is missing in either(get difference in 2 different arrays) using LODASH.
So for eg here are 2 sets of array
array1 = [
[
1564482000000,
30.5
],
[
1564482300000,
null
],
[
1564482000020,
30.5
]
]
array2 = [
[
1564482000000,
30.5
],
[
1564482300000,
null
],
[
1564482000010,
10.5
]
]
In above case Array 1 have following value that Array 2 doesn’t have
[ 1564482000020, 30.5 ]
Similarly , Array 2 have following value that Array 1 doesn’t have
[ 1564482000010, 10.5 ]
Expected Result I am looking to get result of missing value in 2 different arrays so
diff_array2 = [
1564482000020,
30.5
]
diff_array1= [
1564482000010,
10.5
]
I tried XORBY
in Lodash it gives combine value so there is no way to find out which item was missing in Array 1 or Array 2 as the resulting array for XOR is 1 array. Items may or may not exist in either
Code sample here https://repl.it/repls/LateCorruptTypes
You could use differenceBy passing the index of the item to use for comparison as the iteratee:
const diff_array2 = _.differenceBy(array1, array2, 0);
const diff_array1 = _.differenceBy(array2, array1, 0);