Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript sum on property in the sub collection of array of objects

I have an array of objects which each may have collection of transactions I need to extract two values of the array

  • sum value of all objects
  • sum amount of all transactions
[
    {
        "id": "1",
        "value": 2343,
        "transactions": [
            {
                "id": 88,
                "amount": 34,
            },
            {
                "id": 89,
                "amount": 111,
            }
        ]
    },
    {
        "id": "2",
        "value": 345,
        "transactions": [
            {
                "id": 90,
                "amount": 44,
            },
            {
                "id": 91,
                "amount": 112,
            }
        ]
    }
]

The first one I achieve by

objects.reduce((acc, transaction) => acc + transaction.value, 0);

But the second one is too difficult for me to achieve; do I loop and reduce each object?

like image 882
dascorp Avatar asked Mar 08 '23 02:03

dascorp


2 Answers

With lodash you can use _.sumBy() to get the value, and nesting _.sumBy() calls to get the transactions amount:

const data = [{"id":"1","value":2343,"transactions":[{"id":88,"amount":34},{"id":89,"amount":111}]},{"id":"2","value":345,"transactions":[{"id":90,"amount":44},{"id":91,"amount":112}]}];

const value = _.sumBy(data, 'value');

const transactions = _.sumBy(data, ({ transactions }) => _.sumBy(transactions, 'amount'));

console.log('value: ', value);
console.log('transactions: ', transactions);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Without lodash, you can use the same logic with Array#reduce:

const data = [{"id":"1","value":2343,"transactions":[{"id":88,"amount":34},{"id":89,"amount":111}]},{"id":"2","value":345,"transactions":[{"id":90,"amount":44},{"id":91,"amount":112}]}];

const value = data.reduce((acc, { value }) => acc + value, 0);

const transactions = data.reduce((acc, { transactions }) =>   
  transactions.reduce((acc, { amount }) => acc + amount, 0)
, 0);

console.log('value: ', value);
console.log('transactions: ', transactions);
like image 112
Ori Drori Avatar answered Mar 09 '23 17:03

Ori Drori


This may be old fashioned, but I like .forEach() loops for a simple problem like this. They are straightforward and easy to understand.

let objects = [
    {
        "id": "1",
        "value": 2343,
        "transactions": [
            { "id": 88, "amount": 34, },
            { "id": 89, "amount": 111, }
        ]
    },
    {
        "id": "2",
        "value": 345,
        "transactions": [
            { "id": 90, "amount": 44, },
            { "id": 91, "amount": 112, }
        ]
    }
];

let totalValue = 0;
let totalTransactionAmount = 0;

objects.forEach( function( object ) {
    totalValue += object.value;
    object.transactions.forEach( function( transaction ) {
        totalTransactionAmount += transaction.amount;
    });
});

console.log( totalValue, totalTransactionAmount );
like image 39
Michael Geary Avatar answered Mar 09 '23 16:03

Michael Geary