After trying to find many ways to do it here, I’m stuck with this.
I have a Json object and would like to sum all the results.
This code is showing all amounts, but not showing their sum in the end.
var str = e.data.substring(e.data.indexOf("["));
if (IsJsonString(str)) {
var body = JSON.parse(str);
var data = body[1];
if(data && data.id == "ID" && data.some.string == "string"){
var fields= [
data.some
];
var sum = fields.reduce(function(acc, val){
return (+acc.amount) + (+val.amount);
}, {amount: 0});
console.log(sum);
}
}