Home > Software engineering >  How to fill array of boolean values on checkbox change event?
How to fill array of boolean values on checkbox change event?

Time:11-30

What I would like to do is, filling the answers array with boolean values.My checkboxes are populated dynamically but there will be only four of them. If checkbox is unchecked then its value should be false and if checked it should be true.Values should correspond to array index, I mean if first checkbox is switched then only answers[0] should change, if second checkbox is changed then answers[1] and so on..

Sandbox Edit lingering-resonance-7mycm

CodePudding user response:

If I understand you correctly, you can just use the index of your map function to use the correct answer index.

checked={answer[i]}

Setting setAnswers([...answers, e.target.checked]) will append the new new event value to your answer array. Instead you should override the answer value.

answer[i] = e.target.value

  • Related