I attempted to use a few array methods but my solution became quite messy and difficult to follow. I’m in search of a more concise way to do this comparison.
I hit this exact problem building a shopping cart feature. Here’s what worked best for me - using Map objects for better performance, especially with larger datasets. Convert both arrays to Maps using userId as the key, then iterate through to find differences.
This scales way better than nested loops since Map.has() is O(1) vs O(n) with some(). Saw huge performance gains with arrays of thousands of objects. Code stays clean and runs much faster than filter/some combos.
use filter() and some() to find new items: const newItems = updatedList.filter(updated => !originalList.some(orig => orig.userId === updated.userId)). for removed items, just reverse it. it’s a pretty clean way to compare objects.
This works great when you only care about adds and deletes, not changes. Set lookups are fast and the code reads like plain English. Way easier to debug than some() since you can check the Sets separately.