-4

If I have a javascript object like this:

sampleObject[
   {
    suite: "Clubs",
    weight : 10
   },
   {
    suite: "Spades",
    weight : 6
   },
   {
    suite: "Hearts",
    weight : 2
   }
];

How would I go about finding the sum of the weight properties?

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Swink
  • 353
  • 3
  • 26

6 Answers6

6

A simple sum reduction should work.

var sum = sampleObject.reduce(( sum, card ) => sum + card.weight, 0 );

Shilly
  • 8,511
  • 1
  • 18
  • 24
  • Interesting, I didn't know about this last parameter for the reduce method. – Pierre Capo Jul 25 '18 at 14:21
  • The second one is the initial value. This makes the function work on arrays of length 1, which would fail if you don't use the initial value. It's also handy to use a complex accumulator this way without the need for boilerplate checking that. – Shilly Jul 25 '18 at 14:24
  • Thank you for helping, I initially tried a reduce function but my parameters were set to (a,b). I didn't know you can add properties to parameters, thank you again – Swink Jul 25 '18 at 15:09
3

You can map your object then reduce it:

  1. samplObject => [weight0, weight1, weight2]
  2. [weight0, weight1, weight2] => weight0 + weight1 + weight2

This means you transform your initial list into a list of weights, then you sum the values one by one.

const sampleObject = [
  {
    suite: "Clubs",
    weight : 10
  },
  {
    suite: "Spades",
    weight : 6
  },
  {
    suite: "Hearts",
    weight : 2
  }
];

let sum = sampleObject.map( el=> el.weight).reduce( (a,b) => a+b);
console.log(sum)

Output :

18

Note :

In this particular example, map is an overhead. You can calculate the sum easily :

let sum = sampleObject.reduce( (a,b) => a+b.weight,0);

But for more complex data structures, and in general, it's nice to have the concept of map-reduce in mind.

madjaoue
  • 5,104
  • 2
  • 19
  • 31
  • 1
    why `map`? Just overhead. – eisbehr Jul 25 '18 at 14:18
  • 1
    Yes, it's overhead in this minimal example. But If OP has a more complex structure, like nested objects it would be nice to have a general concept like `map-reduce` in mind. – madjaoue Jul 25 '18 at 14:19
  • Then maybe make a better example/description. Just a hint. In the current example it's unnecessary. – eisbehr Jul 25 '18 at 14:21
  • Still don't get it. What do you mean by complex? Even if the value to sum up is somwhere else or deeper located, you could use just `reduce`. For Example `weight: [{value: 10}]` just need to change the reduce operation to `a + b.weight[0].value`. So I would be really interested in an examples whenever `map` is useful here. – eisbehr Jul 25 '18 at 14:30
2

Use reduce method and pass a initial value as 0.You can also use normal for loop or forEach method

let sampleObject = [{
    suite: "Clubs",
    weight: 10
  },
  {
    suite: "Spades",
    weight: 6
  },
  {
    suite: "Hearts",
    weight: 2
  }
];

let sum = sampleObject.reduce(function(acc, curr) {
  //Initially the value of acc will be 0
 // curr is the current object in context
  acc += curr.weight
   return acc;
}, 0)
console.log(sum)
brk
  • 48,835
  • 10
  • 56
  • 78
2

Use Array.reduce

sampleObject = [
   {
    suite: "Clubs",
    weight : 10
   },
   {
    suite: "Spades",
    weight : 6
   },
   {
    suite: "Hearts",
    weight : 2
   }
];

var sum = sampleObject.reduce((a, b) => a + b.weight, 0);

console.log(sum);
romellem
  • 5,792
  • 1
  • 32
  • 64
0

Since you have an array of objects, you can access the first 'weight' variable by doing: sampleObject[0].weight. With that considered, you can sum all of them with a simple for loop:

var sum = 0

for(var i = 0; i < sampleObject.length; i+){
  sum += sampleObject[i].weight
} 
console.log(sum)
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
LJD
  • 498
  • 4
  • 11
0

Use reduce:

let sampleObject = [{
  suite: "Clubs",
  weight : 10
}, {
  suite: "Spades",
  weight : 6
}, {
  suite: "Hearts",
  weight : 2
}];

let sum = sampleObject.reduce((a, b) => a + b.weight, 0);
console.log(sum);
eisbehr
  • 12,243
  • 7
  • 38
  • 63