I have two arrays of objects:
\\offers
[
{DeskUID: "B11A13", Day: 06 Jun 2020}
{DeskUID: "B11A13", Day: 07 Jun 2020}
{DeskUID: "B12B34", Day: 23 Jun 2020}
]
\\reservations
[
{DeskUID: "B11A13", Day: 06 Jun 2020, Name: "Mike"}
{DeskUID: "B12B34", Day: 23 Jun 2020, Name: "Ali"}
]
I would like to have a result where are available offers, that means only the offers without already reserved desks.
\\result
[
{DeskUID: "B11A13", Day: 07 Jun 2020}
]
How to get the difference between two arrays of objects in JavaScript
I already tried solutions on the link above but without success, I just got a result array as sum of all objects from the two arrays.
function comparer(otherArray){
return function(current){
var reserveDay = new Date (current.Day)
return otherArray.filter(function(other){
var offerDay = new Date (other.Day)
return other.DeskUID == current.DeskUID && offerDay == reserveDay
}).length == 0;
}
}
var onlyInA = offers.filter(comparer(reservations));
var onlyInB = reservations.filter(comparer(offers));
result = onlyInA.concat(onlyInB);