David Prifti
David Prifti

Reputation: 711

How to add object values from an object array based on an object key in Javascript

I want to add some object values from an object array based on another object key. I want to know how this can be achieved from plain Javascript or by using a helper library like lodash.

I have already tried using lodash's _.groupBy and Array.prototype.reduce(), but haven't got it to work. Here is what the data looks like:

{
  "2019-01-04": [
    {
        "payments": [
            {
                "sum": "12",
                "currency": "€"
            }
        ]
    }
  ],

  "2019-01-06": [
    {
        "payments": [
            {
                "sum": "50",
                "currency": "€"
            },
            {
                "sum": "30",
                "currency": "£"
            },
            {
                "sum": "40",
                "currency": "Lek"
            },
            {
                "sum": "2",
                "currency": "£"
            },
            {
                "sum": "60",
                "currency": "£"
            }
        ]
    }
  ]
}

I expect a result where the sum property has the sum of all currencies of the same type from that date:

{
  "2019-01-04": [
    {
        "payments": [
            {
                "sum": "12",
                "currency": "€"
            }
        ]
    }
  ],

  "2019-01-06": [
    {
        "payments": [
            {
                "sum": "50",
                "currency": "€"
            },
            {
                "sum": "92",
                "currency": "£"
            },
            {
                "sum": "40",
                "currency": "Lek"
            }
        ]
    }
  ]
}

Upvotes: 0

Views: 127

Answers (2)

Boney
Boney

Reputation: 2202

const result = {};
Object.keys(input).forEach(date => {
    result[date] = [{ }];
    result[date][0].payments = input[date][0].payments.reduce((payments, c) => {
        const grp = payments.find(p => p.currency === c.currency);
        grp ? grp.sum = +grp.sum + +c.sum : payments.push(c);
        return payments;
    }, []);
});

Upvotes: 2

mjrdnk
mjrdnk

Reputation: 233

Given data structure you provided, using following method gives desired output:

function sumByCurrency(history) {
    _.forOwn(history, value => {
        const newPayments = [];

        _.forEach(value["0"].payments, v => {
            let existingPayment = _.find(
                newPayments,
                newPayment => newPayment && newPayment.currency === v.currency
            );

            if (existingPayment) {
                let existingSum = +existingPayment.sum;
                let incomingSum = +v.sum;

                existingSum += incomingSum ? incomingSum : 0;

                existingPayment.sum = "" + existingSum;
            } else {
                newPayments.push({
                    currency: v.currency,
                    sum: v.sum ? v.sum : 0
                });
            }
        });

        value["0"].payments = newPayments;
    });

    return history;
}

Use it passing your object, say you call it paymentHistory to sumByCurrency function like this:

sumByCurrency(paymentHistory);

Please note: You probably want to make some fallbacks/make sure it doesn't break if value["0"].payments is not available.

Upvotes: 0

Related Questions