How do you get a diff between two arrays of object...
# suitescript
k
How do you get a diff between two arrays of objects, returning only the objects that are in only one array?
j
You might have worked this one out by now, but you're looking for something like
Copy code
function isEqual(objX, objY) {
 // need to define an is equal function to compare objects
}
var listA = [{ ... }, { ... }, ...];
var listB = [{ ... }, { ... }, ...];
var onlyInA = listA.filter(a => listB.every(b => !isEqual(a, b));
var onlyInB = listB.filter(b => listA.every(a => !isEqual(a, b));
var diff = onlyInA.concat(onlyInB);
Btw the array methods filter, map, every, some, and forEach are all ES5 and supported by NetSuite
👍🏻 1
For this problem you can't avoid nested loop (without introducing adding more data structures) since you are checking each element in listB for each element in listA and vice versa. For what it's worth the lodash implementation is also a nested loop https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L2773
k
This is very similar to what I ended up with, except I used map instead of filter.
I went with your filter approach because it's a more elegant solution than mine was. Thank you so much!