Mihai Ciobanu
Mihai Ciobanu

Reputation: 155

Add a new property to an object

I am trying to add a new property to an exercise I want to mark as complete, and I can't get the right shape.

This is the reducer:

case COMPLETE_EXERCISE:
      return {
        ...state,
        [action.payload.id]: {
          ...[action.payload.completedExercise],
          complete: true
        }
      };

I want to add the complete: true property to the completedExercise object and to get rid of that 0 index object key so that the Id prop of the completedExercises object contains the name, screenName, text AND the complete props, however the closest I could come up to is to put it next to it:

enter image description here

My current state looks like:

currentstate

and I want it to look like:

enter image description here

Upvotes: 1

Views: 61

Answers (1)

Andreas
Andreas

Reputation: 21881

Just don't wrap action.payload.completedExercise in an array

case COMPLETE_EXERCISE:
      return {
        ...state,
        [action.payload.id]: {
          ...action.payload.completedExercise,
          complete: true
        }
      };

Example:

const foo = { a: 1 };
const bar = {
  ...foo,
  b: 2
};

console.log(bar);

Upvotes: 2

Related Questions