繁体   English   中英

如何到达 json 嵌套的最后一级并动态格式化输出?

[英]How can I get to the last level of a json nest and format the output dynamically?

我有这个json:

[
  {
    "name": "MARVEL",
    "superheroes": "yes"
  },
  {
    "name": "Big Bang Theroy",
    "superheroes": "NO",
    "children": [
      {
        "name": "Sheldon",
        "superheroes": "NO"
      }
    ]
  },
  {
    "name": "dragon ball",
    "superheroes": "YES",
    "children": [
      {
        "name": "goku",
        "superheroes": "yes",
        "children": [
          {
            "name": "gohan",
            "superheroes": "YES"
          }
        ]
      }
    ]
  }
]

我知道如何循环并遍历它,但我需要这样的输出:

[
  {
    "name": "MARVEL",
    "answer": [
      {
        "there_are_superheroes": "YES"
      }
    ]
  },
  {
    "name": "Big Bang Theroy",
    "answer": [
      {
        "there_are_superheroes": "NO",
        "new_leaft": [
          {
            "name": "sheldon",
            "there_are_superheroes": "NO"
          }
        ]
      }
    ]
  },
  {
    "name": "dragon ball",
    "answer": [
      {
        "there_are_superheroes": "YES",
        "new_leaft": [
          {
            "name": "goku",
            "answer": [
              {
                "there_are_superheroes": "YES",
                "new_leaft": [
                  {
                    "name": "gohan",
                    "answer": [
                      {
                        "there_are_superheroes": "YES"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

我试过这样的事情:

format(d) {
if (d.children) {
  d.children.forEach((d) => {
    format;
  });
}
}
format(data);

我不知道如何获得我想要的结构。 我试图用foreach来做,但有一次我不知道如何动态访问直到最后一个children ,这是一个例子,但我可以有n级别,其中可以有更多children元素。 在我的实际项目中,我从 Web 服务获取结构,我需要像这样构建它。

名为superheroes的属性我希望它显示在一个名为answer的数组中,并且在它里面, there_are_superheroes它将有它的值。

 "name": "MARVEL",
 "answer": [
        {
          "there_are_superheroes": "YES",  --> `there_are_superheroes` was `superheroes`,
          "new_leaft": [
                    {
 .
 .

new_leaft相当于children

正如我所说,我知道如何查看对象和数组,但在这种情况下,我不知道如何查看每个对象children nested的最后一个children nested

这是如何进行一级递归:

function format(l){
    const result = {name: l.name};
    result.answer = [{there_are_superheroes: l.superheroes}];
    result.answer[0].new_leaft = l.children;
    return result;
}
format({
      "name": "Big Bang Theroy",
      "superheroes": "NO",
      "children": [
        {
          "name": "Sheldon",
          "superheroes": "NO"
        }
      ]
    })

// result:

{
    "name": "Big Bang Theroy",
    "answer": [
        {
            "there_are_superheroes": "NO",
            "new_leaft": [
                {
                    "name": "Sheldon",
                    "superheroes": "NO"
                }
            ]
        }
    ]
}

如果可能的键列表是固定的,那就很简单了。 否则使用Object.assign复制所有键(或只是遍历那些),然后修改必要的键。

现在您必须弄清楚将递归调用放在哪里(提示: l.children.map(format) ,键是否应命名为treenew_leaf(t) ,检查该字段是否存在并进行相应处理,(请参阅问题在 Javascript 中,如何判断对象中是否存在字段? - 堆栈溢出)等。

暂无
暂无

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

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