I have a json in the below format
let newarrayObj = [
{
"cost one": "118",
"cost two": "118",
"cost three": "118"
},
{
"cost one": "118",
"cost two": "118",
"cost three": "118"
},
{
"cost one": "118",
"cost two": "118",
"cost three": "118"
}
]
I need the sum of each object which is 118 + 188 + 118 = 424 and the sum of total which is 424 + 424 + 424 = 1272
approach so far :
Object.entries(newarrayObj).map(([key, value]) => {
Object.values(value).map((j, i) => {
console.log(j);
})
})
how can i get the expected sum values ?
mapwon't give you a sum (even if you use the return value -- never usemapif you're not using the return value!). See the linked question for how to get the sum of the values in the array you're getting fromObject.values. (Remap: The only reason for using it is to make use the the array it creates and returns. It's not for looping through an array otherwise. See this question's answers for looping through arrays.) The outermapcould make sense, if you return the sum from the callback and use its return value.