Get the Sum of Values in React JS

Viewed 53
data: {
   goodReceiveNote:{ id:1,netTotal: null}
   poDetailsList:[
     0:{id:1,subTotal:300},
     1:{id:1,subTotal:200}
   ]
}

How to get the sum of poDetailsList subTotal and Assign it to the netTotal in goodReceiveNote in react.

1 Answers

Use the array reduce in combinaison with Object.values

const total = Object.values(data.poDetailsList).reduce((a,b) => a + b.subTotal, 0)
Related