Skip to content
Advertisement

Comparing two arrays of objects for matching properties then creating two new arrays

I have the following two arrays:

JavaScript

I need to end up with a newArray containing only one object per Id, but if the ids in an object in the oldOrder array and newOrder array then I need to get the difference in the Qty positive or negative. So for example the above would create this new array:

JavaScript

I would also like to get the dropped orders that are present in the oldOrder Array but not in the newOrder array:

JavaScript

I have already got this logic for getting the dropped orders but I am struggling with creating the newArray and tying the logic into the same function if possible?

JavaScript

Edited to add that I cannot use ESC6 features like spread or fat arrow etc.

Advertisement

Answer

You can easily achieve the result using Map

1) Using ES6

JavaScript
JavaScript

2) Using ES5

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement