j.ExtendArraySum

PHOTO EMBED

Tue Nov 30 2021 23:37:46 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #javascript

class fruitCollection extends Array {
    sum(key) {
        return this.reduce((a, b) => a + (b[key] || 0), 0);
    }
}
const fruit = new fruitCollection(...[
    {  description: 'orange', Amount: 50},
    {  description: 'orange', Amount: 50},
    {  description: 'apple', Amount: 75},
    {  description: 'kiwi', Amount: 35},
    {  description: 'watermelon', Amount: 25 },]);

console.log(fruit.sum('Amount'));
content_copyCOPY

https://www.delftstack.com/howto/javascript/sum-array-of-objects-javascript/