I wanted to use Reduce() method to get the product id and the corresponding amount
apiArray is may array get from ApiRest
const newArray = apiArray.map((items) => {
return {
article_id: items.article_id,
montant : items.montant
}
})
const finalArray = newArray.reduce((acc:any, curr)=>{
if (!acc[curr.article_id]) {
acc[curr.article_id] = '';
}
acc[curr.article_id] += -(-curr.montant) ;
return acc;
},{});
console.log('finalArray==',finalArray)
}
newArraybyarticle_idand sum the data. We have many questions about this process, (e.g., this question). Note that these usually initialize the value to 0 rather than ''...