Home > Software engineering >  Get total sum of price amount multiplied by quantity over nested arrays
Get total sum of price amount multiplied by quantity over nested arrays

Time:02-17

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,
    });

CodePudding user response:

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))

CodePudding user response:

Here is an example with your given data assuming that multiple objects occur in the toplevel object:

const data = [{
    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,
        },
        ],
    },
},];



// function for calculating the total of one object holding tasks and request props.
function calculateTotalPrice(entity) {

    // iterate through all tasks and "reduce" them to one number :)
    return entity.tasks.reduce((total, task) => {

        // Search for a request with the same id!.
        const request = entity.request.tasks.find(request => request.id === task.requestTaskId)

        if (request) {
            return total   (request.quantity * task.price)
        }

        // If no price is found, return current total and log something!.
        console.log('No price found for task:', task)
        return total
    }, 0)
}


// With your object you can now reduce this aswell for getting the grand 
// total over all objects..
const totalPrice = data.reduce((grandTotal, entity) => {
    return grandTotal   calculateTotalPrice(entity)
}, 0)

console.log('totalPrice:', totalPrice)

CodePudding user response:

If your object is ordered as shown with each task marching the corresponding request by index then using .map() and .reduce() methods would achieve your goal:

const total = orders.map(order => order.tasks.reduce(
    (tot, prod, index) => tot   prod.price * order.request.tasks[index].quantity, 0
));

console.log( total );

DEMO

const orders = [{
    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,
            },
        ],
    },
},{
    tasks: [{
            requestTaskId: 'dabee458-8ef0-11ec-912a-3d12c29b6342',
            price: 15,
        },
        {
            requestTaskId: 'dabee502-8ef0-11ec-b81a-3d12c29b6342',
            price: 15,
        },
    ],
    request: {
        tasks: [{
                id: 'dabee458-8ef0-11ec-912a-3d12c29b6342',
                quantity: 15,
            },
            {
                id: 'dabee502-8ef0-11ec-b81a-3d12c29b6342',
                quantity: 50,
            },
        ],
    },
}];

const total = orders.map(order => order.tasks.reduce(
    (tot, prod, index) => tot   prod.price * order.request.tasks[index].quantity, 0
));

console.log( total );

  • Related