javascript - 循环遍历 JSON 子对象

标签 javascript arrays json foreach

所以我循环遍历中间复杂的 json 对象,并在到达递归循环末尾时保存所有值

对象例如

"if": {
    "and": {
        "or": {
            "compare": [
                {
                    "Hello": {
                        "constant": {
                            "string": "1"
                        }
                    },
                },
                {
                    "Hello": {
                        "constant": {
                            "string": "4"
                        }
                    }
                }
            ]
        },
        "before": {
            "Hello2": "12131",
            "Hello": {
                "constant": {
                    "datetime": "2001-01-01T00:00:00"
                }
            }
        }
    }
}

然后我就有了我的功能:

function getAllGroups(obj)
{

        for (var k in obj)
        {
            if (k === "hello") {
                counter++;
                array.push([]);
            }
            if (typeof obj[k] == "object" && obj[k] !== null) {
                getAllGroups(obj[k]);
            }
            else
            {
                if (k !== "hello2") {
                    array[array.length - 1].push(obj[k]);
                }
            }

        }
}

现在可能没有意义,但基本上:

当它找到键“hello”时,我将新对象添加到空数组中,并用数据填充该对象。每次到达键“hello”时,它都会创建新对象并用新数据填充它。

所以我一直坚持保留对父对象的引用。我想要维护的 key 很少,例如“和”与“或”。因此,我的循环的第一个结尾停在 string: 1 我想保持它是“或”的一部分。同样,我想保留“或”是“和”的一部分。最后,日期时间是“and”的一部分。请记住,我可以在其中包含多个“与”和“或”。

编辑:

我已经更改了代码,以便它将保留对父级的引用。不幸的是,它将保留对列表中最后一个父级的引用,因此“datetime”是“and”的子级,但我的代码显示其“or”的子级

function getAllGroups(obj)
{

        for (var k in obj)
        {
            if (k === "and" || k === "or" || k === "not")
            {
                if (parentKey !== "") {
                    array.push([]);
                    array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                    parentKey = k + parentKeyNo;
                    parentKeyNo++;
                    array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                }
                else {
                    parentKey = k + parentKeyNo;
                    parentKeyNo++;
                    array.push([]);
                    array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                }
            }
            if (k === "Hello") {
                counter++;
                array.push([]);
            }
            if (typeof obj[k] == "object" && obj[k] !== null) {
                getAllGroups(obj[k]);
            }
            else
            {
                if (k !== "Hello2") {
                    if (array[array.length - 1].hasOwnProperty('parent'))
                    {
                        array[array.length - 1].push(obj[k]);
                    }
                    else
                    {
                        array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                        array[array.length - 1].push(obj[k]);
                    }

                }
            }

        }
}

期望的结果:

[
    [
        {
            "child": "and"
        }
    ],
    [
        {
            "parent": "and"
        },
        {
            "child": "or"
        }
    ],
    [
        {
            "parent": "or"
        },
        {
            "string": "1"
        }
    ],
    [
        {
            "parent": "or"
        },
        {
            "string": "4"
        }
    ],
    [
        {
            "parent": "and"
        },
        {
            "datetime": "2001-01-01T00:00:00"
        }
    ]
]

最佳答案

在这里,您可以在 process() 函数中处理键和值:

var json = {
  "if": {
    "and": {
      "or": {
        "compare": [{
          "Hello": {
            "constant": {
              "string": "1"
            }
          },
        }, {
          "Hello": {
            "constant": {
              "string": "4"
            }
          }
        }]
      },
      "before": {
        "Hello": {
          "constant": {
            "datetime": "2001-01-01T00:00:00"
          }
        }
      }
    }
  }
};

//called with every property and it's value
var helloValues = [];

function process(key, value) {
  console.log(key + " : " + value);
  if (key == 'Hello') helloValues.push(value);
}

function traverse(o, func) {
  for (var i in o) {
    func.apply(this, [i, o[i]]);
    if (o[i] !== null && typeof(o[i]) == "object") {
      //going on step down in the object tree!!
      traverse(o[i], func);
    }
  }
}

//that's all... no magic, no bloated framework
traverse(json, process);
alert(helloValues.length);

希望这有帮助。

关于javascript - 循环遍历 JSON 子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31298651/

相关文章:

javascript - 如何在 VS Code IDE 中查看文件的历史记录?

javascript - 从数组中删除重复元素(利用对象) - Javascript

java - 在java中将字符串数组转换为 float 数组

php - 在PHP中合并两个json

java - 在Java中将xml转换为json时如何在标签中保留空格?

asp.net-mvc - ASP.NET MVC : Controlling serialization of property names with JsonResult

javascript - Ember.Js 标题重复

javascript - 为什么我的 Google Chrome 扩展只能打开 25 个标签页?

javascript - 如何知道下载何时完成?

java - 递归,每一步导出信息