Alan S
Alan S

Reputation: 93

How can I combine each value from an array of objects?

I'm currently doing a simple dice roll app and I have the following object:

die = [
  {
    ofWhat: 6,
    score: [6]
  },
  {
    ofWhat: 6,
    score: [1] 
  },
  {
    ofWhat: 6,
    score: [5] 
  }
]

I would like to get every score and combine them into one value so I can see the total of every score from this array.

Like this: 6+1+5

How can I achieve this?

What I tried:

total = 0;

this.die.forEach((die) => {
  die.score.forEach((score) => {
    this.total += score;
  });
});

I'm getting NaN

Edit: I made an error in my object

die = [
  {
    ofWhat: 6,
    score: [6]
  },
  {
    ofWhat: 6,
    score: [1] 
  },
  {
    ofWhat: 6,
    score: [5] 
  }
]

total = 0;

this.die.forEach((die) => {
  die.score.forEach((score) => {
    this.total += score;
  });
});

console.log(total)

Upvotes: 0

Views: 156

Answers (10)

bhuvnesh pattnaik
bhuvnesh pattnaik

Reputation: 1463

Try this

        let die = [
          {
            ofWhat: 6,
            score: [6]
          },
          {
            ofWhat: 6,
            score: [1] 
          },
          {
            ofWhat: 6,
            score: [5] 
          }
        ];
        let sum = 0;
        die.forEach(i => {
            i.score.forEach(val => {
                sum += val;
            });
        });
        console.log(sum);

Upvotes: 2

Peter B
Peter B

Reputation: 24137

You can do this this by adding up all the die.score values.

Updated to use arrays (after the question was updated to use arrays)

If there is always just one array item, you can suffice with adding up each die.score[0] value.
Here we calculate 6+1+5 = 12:

var die = [
  { ofWhat: 6, score: [6] },
  { ofWhat: 6, score: [1] },
  { ofWhat: 6, score: [5] }
]

var total = 0;

this.die.forEach((obj) => {
  total += obj.score[0];
});

console.log(total);

Or if there can be multiple values in each array, you'll need to iterate the inner array as well.
Here we calculate 6+6+1+5 = 18:

var die = [
  { ofWhat: 6, score: [6, 6] },
  { ofWhat: 6, score: [1] },
  { ofWhat: 6, score: [5] }
]

var total = 0;

this.die.forEach((obj) => {
  obj.score.forEach((value) => {
    total += value;
  });
});

console.log(total);

Upvotes: 0

Erick Lanford Xenes
Erick Lanford Xenes

Reputation: 1572

Only use score[0]. 'score' is an array.

  var  die = [
  {
    ofWhat: 6,
    score: [6]
  },
  {
    ofWhat: 6,
    score: [1] 
  },
  {
    ofWhat: 6,
    score: [5] 
  }
];
 

   
total = 0;

this.die.forEach((obj) => {
    this.total += obj.score[0];
});

console.log("total score from die array: " + total)

Upvotes: 0

Nick Parsons
Nick Parsons

Reputation: 50674

You could use two .reduce() methods, the inner-most reduce() can be used to get the sum of all the elements in your score array, and the outer most reduce() can be used to sum all the results produced by the inner-most reduce():

const die = [{ofWhat:6,score:[6]},{ofWhat:6,score:[1]},{ofWhat:6,score:[5]}];

const total = die.reduce(
    (sum, {score}) => 
      sum+score.reduce((innerSum, n) => innerSum + n, 0), 0);
    
console.log(total);

Upvotes: 1

Hitesh
Hitesh

Reputation: 167

You could iterate over the array using Map

let die = [
    {
      ofWhat: 6,
      score: 6
    },
    {
      ofWhat: 6,
      score: 1 
    },
    {
      ofWhat: 6,
      score: 5 
    }
 ],sum=0;

die.forEach(x=> sum += x.score);
console.log(sum)

Upvotes: 0

Gokul Ramesh
Gokul Ramesh

Reputation: 11

You can try this :

this.die.forEach((d) => {
  this.total += d.score;
});

Upvotes: 0

Jacek Rojek
Jacek Rojek

Reputation: 1122

die = [
  {
    ofWhat: 6,
    score: 6
  },
  {
    ofWhat: 6,
    score: 1 
  },
  {
    ofWhat: 6,
    score: 5 
  }
]

const total = die.reduce((acc,curr) => acc+curr.score,0)
console.log(total)

Upvotes: 0

Tony
Tony

Reputation: 910

var die = [{
    ofWhat: 6,
    score: 6
  },
  {
    ofWhat: 6,
    score: 1
  },
  {
    ofWhat: 6,
    score: 5
  }
];
var sum = 0;
die.forEach(element => {
  sum += element.score;
});

Upvotes: 1

Mara Black
Mara Black

Reputation: 1751

Since you are taking each object from array with this.die.forEach, the content of object can be accessed like a normal object: object.property

die = [
  {
    ofWhat: 6,
    score: 6
  },
  {
    ofWhat: 6,
    score: 1 
  },
  {
    ofWhat: 6,
    score: 5 
  }
]


total = 0;

this.die.forEach((obj) => {
    this.total += obj.score;
});

console.log("total score from die array: " + total)

Upvotes: 1

David
David

Reputation: 529

You can use a reducer

die = [
  {
    ofWhat: 6,
    score: 6
  },
  {
    ofWhat: 6,
    score: 1 
  },
  {
    ofWhat: 6,
    score: 5 
  }
]


console.log(die.reduce((acc, curr) => {
  acc += curr.score;
  return acc;
}, 0))

Upvotes: 1

Related Questions