javascript

Get total sum of specif attribute inside of array of objects

let products = [{
  "id": 1,
  "product_name": "Forester",
  "price": 10
}, {
  "id": 2,
  "product_name": "Rainier",
  "price": 10
}, {
  "id": 3,
  "product_name": "Yukon Denali",
  "price": 10
}, {
  "id": 4,
  "product_name": "Regal",
  "price": 10
}, {
  "id": 5,
  "product_name": "Alcyone SVX",
  "price": 5
}];

let totalPrice = products.reduce((accumulator, current) => {
          return accumulator + current.price;
        }, 0);

//totalPrice 45

Get total sum of specif attribute inside of array of objects 

Was this helpful?