Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
React Array for Quiz and Answers

I'm trying to create a quiz app with ReactJS. I want to collect and edit answers into an array. Object index gonna be question number.

I can display answers with radio button:

<Radio onClick={() => handleAdd(questionnumber, answer)} label="Fo Answer" />

That's my array and function:

 const [answers, setAnswers] = useState([]);

  const handleAdd = (questionnumber, answer) => {

    const newAnswer = [...answers];
    newAnswer[questionnumber] = answer;
    setAnswers(newAnswer);

  }

I just want to collect all results into an array. When I click radio button of any answer, answer will be added to the array.

enter image description here

For example when I click "Answer one" radio button for Question2. So: "answers[2]: answer one". Then when I click "next" button and select answer for Question3 answers[3] object gonna be changed. If I go back to Question2 again and change my answer from "Answer one" to "Answer two" just edit to answers[2] object.

Final result should be like this:

answers[0]: null
answers[1]: answer of question1
answers[2]: answer of question2
answers[3]: answer of question3
answers[4]: answer of question4
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!