T
T
triggerfinger2018-02-02 16:35:41
JavaScript
triggerfinger, 2018-02-02 16:35:41

How to return and add the values ​​of specific keys in an array?

There is an array

const _products = [
  {'id': 1, 'title': 'iPad 4 Mini', '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 add everything inventoryand display the total amount?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-02-02
@triggerfinger

var inventorires = 0;
_products.forEach(function(product){
  inventorires += product.inventory;
});
alert(inventorires);

R
Rsa97, 2018-02-02
@Rsa97

reduce()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question