I have the following object:
data = [ { name: 'foo', type: 'fizz', val: 9 }, { name: 'foo', type: 'buzz', val: 3 }, { name: 'bar', type: 'fizz', val: 4 }, { name: 'bar', type: 'buzz', val: 7 }, ];
And the lodash card used:
result = _.map(data, function item, idx){ return { key: item[key], values: item.value, } }
Result:
[ { key: foo, val: 9 }, { key: foo, val: 3 }, { key: bar, val: 4 }, { key: bar, val: 7 }, ]
but now I'm trying to return:
[ { key: 'foo', val: 12 }, { key: 'bar', val: 11 }, ]
I tried using a shorthand that seems to only output one object, which I could then convert back to an array, but I feel that there should be an elegant way to use lodash to go from my source data directly to my desired result without any intermediate steps .
I thought this one was affecting my exact problem, but there seems to be quite a bit of work just to convert the object to the desired array of objects described above.
Greetings.