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

123
Views
How to change first array JSON data in Javascript?

Hi I am changing json data using onChange event. The first element of array data is not getting changed & other json data are getting changed. How can we change the first data also ? below is my code -

const onChange = (
        selectType: string,
        selectedValue: string,
        selectedId: number
    ): void => {
        
        const arrayData = arrayData1 // where arrayData1 is state
        
        // get question data to to make the changes
        const myQues = arrayData.find(
            (ques) => ques.quesId=== selectedId
        );

        if (myQues) {
            switch (selectType) {
                case Enum.DEMO_VALUE:

                    myQues.answerList.map((answer) => {

                        if(typeof selectedValue === 'string' && 
                        parseInt(selectedValue) <= answer.high  && parseInt(selectedValue) >= answer.low) {
                            answer.value1 = selectedValue;
                            answer.value2 = selectedValue;
                            answer.answered = true
                        } else {
                            answer.value1 = null;
                            answer.value2 = null;
                            answer.answered = false
                        }
                    });
                
                    break;
            }
        }
        setMyArrayData([...arrayData]);
        
    };

where

answerList = [{
    value1 : null,
    value2 : null,
    answered : false,
    low : 0,
    high : 9
},
{
    value1 : 12,
    value2 : 12,
    answered : true,
    low : 10,
    high : 19
}
{
    value1 : null,
    value2 : null,
    answered : false,
    low : 20,
    high : 300
}]

default answer is 12. When I change value from 12 to 1 then it should go to answerList[0] and rest two should be null and answered should be false and it is working fine also. But when I change value from 1 to 15 again then it should go to answerList[1] and answerList[0] & answerList[2] should be null and answered false but it's not working properly. Both answerList[0] & answerList[1] are same. Again when I change value from 15 to 151 then answerList[0] & answerList[2] are same while answerList[1] gets fine. In short answerList[0] is not getting changed in any condition while answerList[1] & answerList[2] are getting changed. Why is that ? How can we resolve it ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

answerlist[0]["value"] = 1; 

try this sir basically, you want to access the first element in your json array, then in that object you want to change the value property.

more on this here: How can I access and process nested objects, arrays or JSON?

about 4 years ago · Juan Pablo Isaza Report
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!