I am currently working on a task that involves summing values in an array of objects based on the Ids present in another array within the same object. Let's assume I have the following queryArray:
const queryArray = [ "1" , "2" ]
and this is the services array from which I would like to sum the fees property values if their Id matches with queryArray:
const services = [
{
Id: "1",
services: "Early Check-In",
fees: "7500"
},
{
Id: "2",
services: "Late Checkout",
fees: "7500"
},
{
Id: "3",
services: "Airport Chauffeur",
fees: "25000"
}
]
So, I want to calculate the total sum of the fees property value in the services array by matching the Ids available in the queryArray. For example, when using the queryArray:
[ "1" , "2" ]
The expected result should be:
15000
If we use:
[ "1" , "3" ]
The expected result would be:
32500
And for:
[ "1", "2" , "3"]
The expected result should be:
40000
As a beginner in javascript, I am struggling to find a solution or even know where to start. Any guidance or suggestion would be greatly appreciated.