如何使用 React Post Api 替换第二个数组中的项目

How To Replace Items in Second Array Using React Post Api

我有一个 post Api,我想在其中发送基于用户在数组中输入的答案,例如: userAnswer ["correct", "correct", "wrong", "notAttempted", "correct"] 然后根据下一节中的索引替换正确和错误的答案不同的数组是这样的:

{ “answersArray”:[ “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试” ], }

  const [arry, setArry] = useState([]);

  const nextQuestion = (examData) => {

    if (studentAnswered == examData[currentQuestion].correct) {
      setArry((prevState) => [...arry, "correct"]);
    } else if (studentAnswered == null) {
      setArry((prevState) => [...arry, "notAttempted"]);
    } else {
      setArry((prevState) => [...arry, "wrong"]);
    }

    setCurrentQuestion(currentQuestion + 1);
    setStudentAnswered(null);
  };

array 是一个 useState 函数,NextQuestion 是一个 onClick 函数,它将选定的答案添加到数组 onClick 或保留值如果未选择任何选项,则为 null。 **我能够完成大部分工作。现在我只需要根据它们的索引更新 AnswersArray Above 的正确和错误答案。谢谢**

Edit : the first array will store the option that the user selects. Lets say you have 20 questions and the user answers 5 questions. userAnswer ["correct", "correct", "wrong", "notAttempted", "correct"]. the correct and wrong answers need to be pushed into the second array above in which all of the indexes have "notAttempted" value stored. So my question is how can I update the values in Array2 with the values in array1 which is userAnswer. The answersArray should look like this :

{ “answersArray”:[ “正确的”, “正确的”, “错误的”, “没有尝试”, “正确的”, “没有尝试”, “没有尝试”, “没有尝试”, “没有尝试” ], }

userAnswer 中的值将根据索引号推送到 answersArray 中。我希望这能让问题更清楚。

尝试使用数组方法通过索引替换数组中的任何值。