简体   繁体   中英

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

I have this 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 . In my real project I am getting a structure from a web service, I need to structure it like this.

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.

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

and new_leaft is the equivalent of 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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