How to return and add up the values of certain keys in the array?

There is an array of

const _products = [
 {'id': 1, 'title': 'iPad Mini 4', 'price': 500.01, 'inventory': 2},
 {'id': 2, 'title': 'H&M T-Shirt White', 'price': 10.99, 'inventory': 10},
 {'id': 3, 'title': 'Charli XCX - Sucker CD', 'price': 19.99, 'inventory': 5}
]


How to put all the inventory and display the total amount?
June 8th 19 at 16:43
2 answers
June 8th 19 at 16:45
Solution
var inventorires = 0;
_products.forEach(function(product){
 inventorires += product.inventory;
});
alert(inventorires);
It doesn't work

productsCount (state) {
 let count = 0
 state.products.forEach((product) => {
 count += product.inventory
 return count
})
 }
- elliott.Cartwrig commented on June 8th 19 at 16:48
return in the wrong place put - Edwin.Abbo commented on June 8th 19 at 16:51
,
productsCount function (state) {
 let count = 0
 state.forEach((product) => {
 count += product.inventory;
});
 return count;
}
- nikolas.Reichert commented on June 8th 19 at 16:54
Similarly, ATP - elliott.Cartwrig commented on June 8th 19 at 16:57
June 8th 19 at 16:47

Find more questions by tags JavaScript