I have nested array of objects like this:
[
{
tasks: [
{
requestTaskId: 'be6ee458-8ef0-11ec-912a-3d12c29b6342',
price: 5,
},
{
requestTaskId: 'be6ee502-8ef0-11ec-b81a-3d12c29b6342',
price: 5,
},
],
request: {
tasks: [
{
id: 'be6ee458-8ef0-11ec-912a-3d12c29b6342',
quantity: 10,
},
{
id: 'be6ee502-8ef0-11ec-b81a-3d12c29b6342',
quantity: 10,
},
],
},
},
];
And I need to sum all the price and quantity of the tasks, also checking/matching by tasks.requestTaskId === request.tasks.id
condition.
The result should be (10 * 5) + (10 * 5) = 100.
How to get this using map
/find
/ reduce
? Can't think of better helpers to achieve this, just don't know how to combine everything.
This is what i tried:
array.map((item) => ({
...item,
...item.request.tasks.find((requestTask) => ({
...requestTask,
...item.tasks.find(({ requestTaskId }) => requestTaskId === requestTask.id),
})),
}))
.map(({ amount, quantity})) => ({
total: amount * quantity,
});
You could use Array.reduce()
to sum the total price for all tasks, first calling on the input array, then on the tasks.
If no requestTask
is found for a task we'll simply leave the total as it is. You could change this behaviour to throw an error if you feel this better reflects your use case.
We'd wrap this in a getTotalSum()
function.
let input1 = [ { tasks: [ { requestTaskId: 'be6ee458-8ef0-11ec-912a-3d12c29b6342', price: 5, }, { requestTaskId: 'be6ee502-8ef0-11ec-b81a-3d12c29b6342', price: 5, }, ], request: { tasks: [ { id: 'be6ee458-8ef0-11ec-912a-3d12c29b6342', quantity: 10, }, { id: 'be6ee502-8ef0-11ec-b81a-3d12c29b6342', quantity: 10, }, ], }, }, ];
let input2 = [ { tasks: [ { requestTaskId: '4a0cc1d8-9720-48c4-9678-3372c20ff7c3', price: 10, }, { requestTaskId: '6c178a61-e9aa-45bf-bcde-1a52bcd29205', price: 1, }, ], request: { tasks: [ { id: '4a0cc1d8-9720-48c4-9678-3372c20ff7c3', quantity: 2, }, { id: '6c178a61-e9aa-45bf-bcde-1a52bcd29205', quantity: 3, }, ], }, }, ];
function getTotalSum(input) {
return input.reduce((total, { tasks, request }) => {
return tasks.reduce((total, task) => {
const requestTask = request.tasks.find(({ id }) => id === task.requestTaskId);
total += requestTask ? task.price * requestTask.quantity: 0;
return total;
}, total)
}, 0)
}
console.log('Sum1:', getTotalSum(input1))
console.log('Sum2:', getTotalSum(input2))