dimanche 11 octobre 2020

Failure to update nested array based on parent array index

I have an array (answers) which contains two elements, one an integer(questionNo) and another an array(answer). I can successfully push new data elements to the array but when I attempt to update the nested array based on the index of the parent array and the value of the integer, i instead end up updating all the nested arrays. Updating only works if there is a single data set (parent array index 0)

The function is:

updateQnAnswered(qNumber, answerArray){

    for(let i = 0; i < this.answers.length; i++) 
    {
        if(this.answers[i].questionNo === qNumber)
        {
            console.log("update " + i);
            this.answers[i].answer = answerArray;
        }
    }
            
}

Scenario:

updateQnAnswered(2, [true, false]) would be expected to loop through the answers array until it finds a match for this.answers[i].questionNo === 2 then update this.answers[i].answer = [true, false] but instead of updating the particular array associated with matched questionNo, all the arrays are updated instead. Updating only works with index 0 of the parent array

And after index 0, all the nested arrays get updated instead of the intended array linked to questionNo.

Aucun commentaire:

Enregistrer un commentaire