CraZyDroiD
CraZyDroiD

Reputation: 7105

For loop inside switch case

I want to run a for loop inside one of my switch case.

case POINTS:
      return {

        ...state,
        totalPoints: action.user_points[0].singular_point


      }

So here action.user_points[0] has to start with 0 and should go all way upto 10. How can i do this?

Upvotes: 1

Views: 94

Answers (2)

Tholle
Tholle

Reputation: 112897

You could slice out the first 10 elements in the array and then reduce all the singular_point values into one value.

case POINTS:
  return {
    ...state,
    totalPoints: action.user_points
      .slice(0, 10)
      .reduce((acc, point) => acc + point.singular_point, 0)
  };

Upvotes: 3

tam.dangc
tam.dangc

Reputation: 2032

I just do the syntax and make an example. You can fix the code follow your requirement

case POINTS: {
  let totalPoints = 0
  for(let i = 0; i<=10; i++) {
    totalPoints += action.user_points[i]
  }
  return {
    ...state,
    totalPoints
  }
}

Upvotes: 0

Related Questions