I have two sets of data represented as arrays:
var oldOrder = [{"Id": "10","Qty": 1}, {"Id": "3","Qty": 2}, {"Id": "9","Qty": 2}];
var newOrder = [{"Id": "5","Qty": 2},{"Id": "10","Qty": 3}, {"Id": "9","Qty": 1}];
I want to merge these arrays into a new one, keeping only one object per Id. If an Id exists in both oldOrder and newOrder, I need to calculate the difference in Qty. For example, combining the above arrays would result in:
var newArray = [{"Id": "5","Qty": 2},{"Id": "10","Qty": 2}, {"Id": "9","Qty": -1}];
Additionally, I want to identify any dropped orders that are in oldOrder but not in newOrder:
var droppedOrders = [{"Id": "3","Qty": 2}];
I have a function to find dropped orders, but I need help incorporating the logic to create the newArray within the same function if possible:
function comparer(otherArray){
return function(current){
var onlyInOld = otherArray.filter(function(other){
return other.Id == current.Id;
}).length == 0;
return onlyInOld;
}
}
var droppedOrders= oldOrder.filter(comparer(newOrder));
console.log(droppedOrders);
It's worth noting that I am unable to use ES6 features like spread or fat arrow functions.