I am working with 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"}
]
My goal is to find the available offers, which are only the offers that have not been reserved yet.
\\result
[
{DeskUID: "B11A13", Day: 07 Jun 2020}
]
Here is a solution I found on Stack Overflow related to getting the difference between two arrays of objects in JavaScript
Even after trying various solutions from the provided link, I have not been successful. The result I get is always the sum of all objects from both 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);