0

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)

8
  • Don't you want a reduce function for this? w3schools.com/jsref/jsref_reduce.asp Commented Oct 31, 2019 at 12:50
  • Shouldn't this this.total += score; not be total += score;? Commented Oct 31, 2019 at 12:51
  • 1
    How are you getting NaN? die.score.forEach should throw an error Commented Oct 31, 2019 at 12:51
  • @adiga I'm using TypeScript Commented Oct 31, 2019 at 13:02
  • 1
    Created a snippet with your code and it works as intended. Commented Oct 31, 2019 at 13:23

10 Answers 10

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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))

Comments

1

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)

1 Comment

Sorry, I made an error in my object the score is actually an array of number
1

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

Comments

0

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)

Comments

0

You can try this :

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

Comments

0

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)

Comments

0

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)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.