I tried to get the sum of this JSON array values. But I don't know the right way to to do this.
var taxes = [ {"1": 11}, {"2": 33} ];
result = 44;
You can use .reduce() to calculate sum like this:
let taxes = [{"1":11}, {"2":33}];
let result = taxes.reduce((a, c) => a + c[Object.keys(c)], 0);
console.log(result);
In case your objects are having consecutive numbers as properties you can use a simpler approach like:
let taxes = [{"1":11}, {"2":33}];
let result = taxes.reduce((a, c, i) => a + c[i + 1], 0);
console.log(result);
c[[Object.keys(c)]] works in that example? Why double brackets?You can try with Array.prototype.map() and Array.prototype.reduce()
var taxes = [{"1":11},{"2":33}];
const result = taxes.map(a=>Object.values(a)).reduce((a,b)=>parseInt(a)+parseInt(b))
console.log("result = "+result);
reduceand then provide what you've tried and what doesn't work. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…