Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在对象中递归更改值?_Javascript_Recursion - Fatal编程技术网

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

Javascript 如何在对象中递归更改值?,javascript,recursion,Javascript,Recursion,我有以下对象,我想将所有选项的键值从“是”、“否”更改为“显示”、“隐藏” 我曾尝试创建一个递归函数来循环对象子项-但由于某些原因,我无法使其完成工作 var foo = { "data": [ { "body": [ { "id": "title",

我有以下对象,我想将所有选项的键值从“是”、“否”更改为“显示”、“隐藏”

我曾尝试创建一个递归函数来循环对象子项-但由于某些原因,我无法使其完成工作

            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)
我的目的是操纵对象,使其自身随着选择的变化而返回。。。。 谢谢


代码太大太复杂了。你可以很容易地这样做, 让数据是

 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) {
  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))

请学习。@str我的意思是物体…改变它…谢谢。