我试图在下面的减速器中更新状态。

结果对象具有两个数组:

results: {
    correctAnswers: [7]
    incorrectAnswers: (9) [0, 1, 2, 3, 4, 5, 6, 8, 9]
}


如何将totalScore添加到结果对象?下面的代码添加totalScore,但是删除correctAnswersincorrectAnswers

case "COMPLETE_QUIZ": {
  const { totalScore, id } = action.data;
  console.log(action.data, state);
  return {
    videos: state.videos.map(video =>
      video.id === id
        ? {
            ...video,
            results: {
              totalScore \\ add here
            },
            completed: true
          }
        : video
    ),
    search: { term: "", videos: [] }
  };
}

最佳答案

spread原始对象并添加新属性

results: {
    ...state.results,
    totalScore
},

08-28 12:49