如何与嵌套数组进行比较并在JavaScript中更新值?

时间:2019-01-28 11:50:38

标签: javascript node.js angularjs ecmascript-6 underscore.js

我想将下面的inputArr与arrObj1进行比较。在下面的数组中,我具有questionId和optionId,通过这些键,我想在arrObj1数组中查找并更新值,并且需要在其中按下键“ isSelected:true” arrObj1,数组有两种类型,一种是嵌套的,另一种是正常的,在arrObj1中,第一个数组是嵌套的,第二种是正常的,我将isSelected键加粗,即需要在其中按下键。

    var inputArr = {
    "questionInfo": [

        {
        "questionId": "400003",
        "quesOptions": [
            {
            "optionId": "400004",
            "status": 1
            },
            {
                "optionId":"400032",
                "status":1
            }
        ]
        },
        {
        "questionId": "300001",
        "quesOptions": [
            {
            "optionId": "300014",
            "status": 1
            }
        ]
        }
    ]
    }


    var arrObj1 = {
    "questionOptions": [
        {
        "questionId": "300001",
        "options": [
            {
            "optionId": "300037"
            },
            {
            "optionId": "300014"
            }
        ],
        "isDisplayable": true
        },
        {
        "questionId": "300004",
        "options": [
            {
            "optionId": "400035",
            "questionOptions": [
                {
                "questionId": "400005",
                "options": [
                    {
                    "optionId": "400001",
                    "questionOptions": [
                        {
                        "questionId": "400003",
                        "options": [
                            {
                            "optionId": "400004",
                            "isDisplayable": true
                            },
                            {
                            "optionId": "400032"
                            },
                            {
                            "optionId": "400026"
                            },
                            {
                            "optionId": "400027",
                            }
                        ]
                        }
                    ]
                    }
                ],
                "optionsHistory": [
                    {
                    "optionId": "400002"
                    }
                ]
                }
            ]
            }
        ]
        },
        {
        "questionId": "300005",
        "options": [
            {
            "optionId": "300026"
            },
            {
            "optionId": "300034"
            },
            {
            "optionId": "300006"
            },
            {
            "optionId": "300017"
            }
        ]
        }
    ]
    }

以下是我的预期输出

    {
    "questionOptions": [
        {
        "questionId": "300001",
        "options": [
            {
            "optionId": "300037"
            },
            {
            "optionId": "300014",
            **"isSelected":true**
            }
        ],
        **"isSelected":true,**
        "isDisplayable": true
        },
        {
        "questionId": "300004",
        **"isSelected":true,**
        "options": [
            {
            "optionId": "400035",
            **"isSelected":true,**
            "questionOptions": [
                {
                "questionId": "400005",
                **"isSelected":true,**
                "options": [
                    {
                    "optionId": "400001",
                    **"isSelected":true,**
                    "questionOptions": [
                        {
                        "questionId": "400003",
                        **"isSelected":true,**
                        "options": [
                            {
                            "optionId": "400004",
                            **"isSelected":true,**
                            "isDisplayable": true
                            },
                            {
                            "optionId": "400032",
                            **"isSelected":true**
                            },
                            {
                            "optionId": "400026"
                            },
                            {
                            "optionId": "400027"
                            }
                        ]
                        }
                    ]
                    }
                ],
                "optionsHistory": [
                    {
                    "optionId": "400002"
                    }
                ]
                }
            ]
            }
        ]
        },
        {
        "questionId": "300005",
        "options": [
            {
            "optionId": "300026"
            },
            {
            "optionId": "300034"
            },
            {
            "optionId": "300006"
            },
            {
            "optionId": "300017"
            }
        ]
        }
    ]
    }

我尝试了以下

    for(var i =0;i<myObj.questionOptions.lenght;i++){
        for(var j=0;j<myObj.questionOptions.options.lenght;j++){
            if(arr[i].optionId === myObj.questionOptions.optionId){
                myObj.questionOptions.options.isSelected = true;
            }
            else{

            }
        }
    }   

任何人都给我一个解决方案。

0 个答案:

没有答案