简体   繁体   English

如何在对象中递归更改值?

[英]How to change values recursively in object?

I have the following object , and i want to change all the "choices" key values from "yes,no" to "show, hide".我有以下对象,我想将所有“选择”键值从“是,否”更改为“显示,隐藏”。

I've tried to create a recursion function to loop over the object sub items - but for some reason i can't get it the work...我试图创建一个递归函数来循环对象子项 - 但由于某种原因我无法得到它的工作......

            var foo = {
            "data": [
                {
                    "body": [
                        {
                            "id": "title",
                            "label": "title",
                            "subType": "title",
                            "type": "text"
                        },
                        {
                            "id": "page1",
                            "label": "Home",
                            "choices": [
                                "show",
                                "hide"
                            ],
                            "childrenItems": [
                                {
                                    "id": "subMenu1",
                                    "label": "Gallery",
                                    "choices": [
                                        "yes",
                                        "no"
                                    ],
                                    "childrenItems": [
                                        {
                                            "id": "subMenu2",
                                            "label": "Artists",

                                            "choices": [
                                                "yes",
                                                "no"
                                            ],
                                            "childrenItems": [
                                                {
                                                    "id": "modernisem",
                                                    "label": "Modernisem"

                                                },
                                                {
                                                    "id": "contemporery",
                                                    "label": "Contemporery"

                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }

        function setQuestionnaireChoises(prmData) {

        return getNestedElements(prmData)
        }

        function getNestedElements(prmData){
        if(!isObject && Array.isArray(prmData)){
            return getArrItems(prmData);
        }else{
            return getObjItems(prmData);
        }
        }

        function getArrItems(prmData){

            for (let index = 0; index < prmData.length; index++) {
                let arrItem = prmData[index];
                if(isObject){
                return getObjItems(arrItem)
                }else{
                    return getArrItems(arrItem)
                }

            }
        }

        function getObjItems(prmData){
            for (const key in prmData) {
                    let objItem = prmData[key];
                    if(key == 'choices'){
                    return objItem[key] = ["show, hide"]
                    }else{
        return  objItem[key]
                    }


            }

        }
        function isObject(prmObj) {
            return prmObj !== null && typeof prmObj === 'object' && Array.isArray(prmObj) === false ? true : false;
        }


        setQuestionnaireChoises(foo)

My purpose is to manipulate the object to return itself with the changes of the choices.... Thanks.我的目的是操纵对象随着选择的变化返回自身......谢谢。

JsBin贾斌

The code is too large and complex.代码太大,太复杂。 You can easily do in this way, Let the data is你可以用这种方式轻松做到,让数据

 var foo = {
  "data": [
    {
      "body": [
        {
          "id": "title",
          "label": "title",
          "subType": "title",
          "type": "text"
        },
        {
          "id": "page1",
          "label": "Home",
          "choices": [
            "show",
            "hide"
          ],
          "childrenItems": [
            {
              "id": "subMenu1",
              "label": "Gallery",
              "choices": [
                "yes",
                "no"
              ],
              "childrenItems": [
                {
                  "id": "subMenu2",
                  "label": "Artists",

                  "choices": [
                    "yes",
                    "no"
                  ],
                  "childrenItems": [
                    {
                      "id": "modernisem",
                      "label": "Modernisem"

                    },
                    {
                      "id": "contemporery",
                      "label": "Contemporery"

                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

And you can change the choice like this你可以像这样改变选择

function setQuestionnaireChoises(prmData) {
  if (!(typeof prmData === 'object' || Array.isArray(prmData))) {
    return
  }
  if (prmData.choices) {
    prmData.choices = ['show', 'hide'];
  }
  Object.keys(prmData).forEach((key) => {
    setQuestionnaireChoises(prmData[key])
  });
}

setQuestionnaireChoises(foo);
console.log(JSON.stringify(foo))

JsBin code JsBin 代码

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

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