DEV Community

VIJAY R B
VIJAY R B

Posted on

I want to flat this json as array.Any help pls

let json = [ {

    "name":"vay",

    "age":26,

    "children":[

         {"name":"Ajay",

          "age":16,

          "children":[

              {

            "name":"kojay",

            "age":26,

            "children":[

                {

                    "name":"dija",

                    "age":"4"

                }

            ]

         }

          ]

         },

         {

            "name":"kojay",

            "age":26,

            "children":[

                {

                    "name":"dija",

                    "age":4

                }

            ]

         }

    ]

}]
Enter fullscreen mode Exit fullscreen mode

`let array=[]

function r(json){

for(let i=0;i<json.length;i++){

array.push(json[i])

  function e(jso){

    if(json[i].children!=null){

        json[i].children.map((x)=>array.push(x))

    }

}

e(json[i])



}
Enter fullscreen mode Exit fullscreen mode

}
`

Top comments (0)