I am developing this quiz app using React.js which I fetch the questions from an API. Here is the first question from the JSON file I extracted from the API:
export default [{
"response_code": 0,
"results": [
{
"category": "Science: Computers",
"type": "multiple",
"difficulty": "medium",
"question": "Which of these is the name for the failed key escrow device introduced by the National Security Agency in 1993?",
"correct_answer": "Clipper Chip", //I want to copy this to the "incorrect_answers" array
"incorrect_answers": [
"Enigma Machine",
"Skipjack",
"Nautilus"
//Clipper Chip
]
// So I'll be able to have 4 buttons with options to choose from,
// while having the "correct_answer" switch position but still retain its value
},
{
"category": "Science: Computers",
"type": "multiple",
"difficulty": "medium",
"question": "In the server hosting industry IaaS stands for...", ...
Here is what I want to do, I want to move or copy the "correct_answer" into the "incorrect_answers" array so I'll be able to have 4 options on each question when I render the JSX. Here is my code to the above:
data.incorrect_answers.push(data.correct_answer)
const answersElement = data.incorrect_answers.map((answer) => {
console.log(answer)
return (
<>
<button>
{answer}
</button>
</>
)
})
The problem with this "PUSH" method is that it duplicates the "correct_answer" buttons and doubles on each click, which is weird to me.
Remember, this data comes from an API so I can't modify it manually to suit me. I also don't want all the correct answer options to be in the same position as all the questions, I want to be able to swap their position.
Note: I have 5 questions with 4 options on each fetch call.