Below is my JSON object (Below is just example of values I am interested in , in reality along with cost there are many other paramters) I am trying to do this in experss js and using underscore
[
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
I want the output as below
Joe 950
Tam 500
I tried this:
var iw={};
iw = Object.keys(myJsonObject.reduce((iw, curr) => {
//iw[curr.author.id] = iw[curr.author.id]
iw[curr.author.id].cost += parseInt(curr.cost);
return iw;
}, iw)).map(key => iw[key]);
console.log("New ::"+iw);
But I didn't get what I hoped for:
TypeError: Cannot read property 'cost' of undefined
at Object.keys.myJsonObject.reduce (repl:3:7)
at Array.reduce (<anonymous>)
New ::[object Object]