Home > Software design >  Convert nested Array of Objects to one dimensional Array of Objects Javascript/React
Convert nested Array of Objects to one dimensional Array of Objects Javascript/React

Time:07-23

I have a nested array of objects which I need to convert to one dimensional Array of objects. In the final Array the 'childrens' is not required.

Original Array

const data = [ { title: "Dashboard", icon: "Dashboard", page:"<Dashboard./>", path: "dashboard"

},
{
    title: "Management",
    icon: "Management",
    page:"<Management/>",
    path: "manage",
    childrens:[
        {
            title: "Assets",
            icon: "Dot",
            page:"<Assetshome/>",
            path: "assets",
            childrens:[
        
                {
                    title: "MyAssets",
                    icon: "Dot",
                    page:"<Myassets/>",
                    path: "myassets"

                },
                {
                    title: "AddAssets",
                    icon: "Dot",
                    page:"<Addassets/>",
                    path: "addassets"

                }                    
            ]
        },
        {
            title: "Users",
            icon: "Dot",
            page: "<Users/>"
            path: "users"
        },
        {
            title: "Office",
            icon: "Dot",
            page:"<Office/>"
            path: "office"
        }            

    ]
},
{
    title: "Reports",
    icon: "Reports",
    page:"<Reports/>"
    path: "reports"
} 

]

Required Array const newdata = [ { title: "Dashboard", icon: "Dashboard", page:"", path: "dashboard"

},
{
    title: "Management",
    icon: "Management",
    page:"<Management/>",
    path: "manage"
},
{
    title: "Assets",
    icon: "Dot",
    page:"<Assetshome/>",
    path: "assets"
},
        
{
    title: "MyAssets",
    icon: "Dot",
    page:"<Myassets/>",
    path: "myassets"

},
{
    title: "AddAssets",
    icon: "Dot",
    page:"<Addassets/>",
    path: "addassets"

},               
       
{
    title: "Users",
    icon: "Dot",
    page: "<Users/>"
    path: "users"
},
{
    title: "Office",
    icon: "Dot",
    page:"<Office/>"
    path: "office"
},   
{
    title: "Reports",
    icon: "Reports",
    page: "<Reports/>"
    path: "reports"
} 

]

What is the best way to do this in javascript.

CodePudding user response:

const flatten = (source, list) => {
    if (list == undefined) {
        list = []
    }
    for (objs of (source || [])) {
        let {childrens, ...item} = objs
        list.push(item)
        flatten(childrens, list)
    }
    return list
}

const newdata = flatten(data)

Note that the childrens variable above uses the object destructuring assignment, so it needs to have the same name as the object key for your child arrays. (The other variable names aren't important).

I tested it on your source data and it seems to work, once I fixed the missing commas in your supplied json.

CodePudding user response:

Possible solution

function extractRecursive(current) {
  const { childrens, ...props } = current;
  const data = [{ ...props }];
  if (current.childrens) {
    current.childrens.forEach((innerChild) => {
      const innerChilds = extractRecursive(innerChild);
      data.push(...innerChilds);
    });
  }
  return data;
}

const flattenData = [];
data.forEach(obj => {
  flattenData.push(
    ...extractRecursive(obj)
  )
})

Stackblitz Snippet

  • Related