简体   繁体   English

如何在 Javascript 中更改第一个数组 JSON 数据?

[英]How to change first array JSON data in Javascript?

Hi I am changing json data using onChange event.嗨,我正在使用 onChange 事件更改 json 数据。 The first element of array data is not getting changed & other json data are getting changed.数组数据的第一个元素没有改变,其他 json 数据也在改变。 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 .默认答案是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.当我将值从12更改为1它应该转到answerList[0]并且其余两个应该是空的,并且answered应该是假的,它也可以正常工作。 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.但是,当我再次将值从1更改为15时,它应该转到answerList[1]并且answerList[0] & answerList[2]应该为空并answered false 但它无法正常工作。 Both answerList[0] & answerList[1] are same. answerList[0]answerList[1]都是一样的。 Again when I change value from 15 to 151 then answerList[0] & answerList[2] are same while answerList[1] gets fine.同样,当我将值从15更改为151 answerList[0]answerList[2]相同,而answerList[1]变好了。 In short answerList[0] is not getting changed in any condition while answerList[1] & answerList[2] are getting changed.简而言之, answerList[0]在任何情况下都不会改变,而answerList[1]answerList[2]正在改变。 Why is that ?这是为什么 ? How can we resolve it ?我们该如何解决?

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.基本上试试这个先生,您想访问 json 数组中的第一个元素,然后在该对象中您想更改 value 属性。

more on this here: How can I access and process nested objects, arrays or JSON?更多相关信息: 如何访问和处理嵌套对象、数组或 JSON?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM