0

I have an array with few objects. every obect has one key with a number.

I need to sum all those numbers

for example:

Users = [ 

{name: example, age: 10},
{name: example2, age: 30},
{name: example3, age: 15}

]

thats what I tried:

  var sum = 0;
     for (let i = 0; i < this.shippingMethod.length; i++) {
     var currentValue = this.shippingDetails.shippingQuantity
     sum += currentValue;
     console.log(sum)
     return sum
}
Bar Levin
  • 205
  • 1
  • 12
  • Possible duplicate of [Better way to sum a property value in an array](https://stackoverflow.com/questions/23247859/better-way-to-sum-a-property-value-in-an-array) – Gecko Jul 04 '19 at 09:07

1 Answers1

0

Try this:

Users = [ 

{name: "Max", age: 10},
{name: "Fritz", age: 30},
{name: "Tom", age: 15}

]

var sum = 0;

Users.forEach(function(user) {
  sum += user.age;
});

console.log(sum)
Vasi G.
  • 161
  • 8