1

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.

yavg
  • 2,761
  • 7
  • 45
  • 115
  • 4
    You'll want to use recursion for that. –  Sep 29 '20 at 22:45
  • @ChrisG I also tried something like that, but with the output I need I don't know how to do it, I have tried many things but it is c – yavg Sep 29 '20 at 22:46
  • If you don't know how to recurse, at least post your code to handle the topmost layer. Your description is pretty confusing. – user202729 Sep 29 '20 at 23:35
  • @user202729 Excuse me, it is complicated to explain, and my native language is not English, in the same way I put the expected output, but as I said, even with recursion, I don't know how to get the structure I want – yavg Sep 29 '20 at 23:40
  • I mean, can you write code to process only the topmost layer? If you're not very familiar with recursion at least you can do this? – user202729 Sep 29 '20 at 23:43
  • @user202729 I put the code I do the recursion with, I have updated my question. but as I tell you, I don't know how to form the expected output – yavg Sep 29 '20 at 23:45

1 Answers1

0

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.

user202729
  • 3,358
  • 3
  • 25
  • 36