Search code snippets, questions, articles...

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 

Search Index Data (The code snippet can also be found with below search text)

Get total sum of specif attribute inside of array of objects
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet