I'm trying to return an array of objects that contain the values from another object in the array that has the same path
value.
My current array:
updates: [
{
path: "foo",
value: {
currency: "USD",
},
},
{
path: "foo",
value: {
amount: "10000",
},
},
{
path: "bar",
value: {
currency: "AED",
},
},
{
path: "bar",
value: {
amount: "10",
},
},
]
Array output I'm looking for:
updates: [
{
path: "foo",
value: {
currency: "USD",
amount: "10000"
},
},
{
path: "bar",
value: {
currency: "AED",
amount: "10"
},
},
]
Probably a simple solution here but I can't figure it out. Thanks!
Tried using lodash:
var result = _(updates)
.groupBy('path')
.map(_.spread(_.assign))
.value();
But didn't get the combined values of the nested objects, instead I got:
[
{ action: 'set', path: 'fellowStipend', value: { amount: '10000' } },
{
action: 'set',
path: 'professionalDevelopmentAllowance',
value: { amount: '10' }
}
]