简体   繁体   English

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

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

I have this json:我有这个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"
          }
        ]
      }
    ]
  }
]

I know how to loop and go through it but I need an output like this:我知道如何循环并遍历它,但我需要这样的输出:

[
  {
    "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"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

I tried something like this:我试过这样的事情:

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

I don't know how to get the structure I want.我不知道如何获得我想要的结构。 I have tried to do it with foreach , but at one point I don't know how to dynamically access until the last children , this is an example but I can have n levels where there can be elements with more children .我试图用foreach来做,但有一次我不知道如何动态访问直到最后一个children ,这是一个例子,但我可以有n级别,其中可以有更多children元素。 In my real project I am getting a structure from a web service, I need to structure it like this.在我的实际项目中,我从 Web 服务获取结构,我需要像这样构建它。

the attribute called superheroes I want it to be shown inside an array called answer and inside of it, there_are_superheroes it will have its value.名为superheroes的属性我希望它显示在一个名为answer的数组中,并且在它里面, there_are_superheroes它将有它的值。

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

and new_leaft is the equivalent of childrennew_leaft相当于children

As I said, I know how to go through objects and arrays but in this case, I don't know how to go to the last children nested of each object.正如我所说,我知道如何查看对象和数组,但在这种情况下,我不知道如何查看每个对象children nested的最后一个children nested

This is how to do one level of recursion:这是如何进行一级递归:

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"
                }
            ]
        }
    ]
}

If the list of possible keys is fixed, it's very simple.如果可能的键列表是固定的,那就很简单了。 Otherwise use Object.assign to copy all the keys (or just iterate through those ), then modify the necessary keys.否则使用Object.assign复制所有键(或只是遍历那些),然后修改必要的键。

Now you have to figure out where to put the recursion calls (hint: l.children.map(format) , whether the key should be named tree or new_leaf(t) , check if the field exists and handle accordingly, (See the question In Javascript. how can I tell if a field exists inside an object? - Stack Overflow ), etc.现在您必须弄清楚将递归调用放在哪里(提示: l.children.map(format) ,键是否应命名为treenew_leaf(t) ,检查该字段是否存在并进行相应处理,(请参阅问题在 Javascript 中,如何判断对象中是否存在字段? - 堆栈溢出)等。

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

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