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"
}
]
}
]
}