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'));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter