How to turn an Array of objects into an object?

Hello!
I want to have this output:

{ name: "Saviole", role: "ceo", children:[{ name: "Mary", role: "supervisorA" , children: [{name: "Anna", role: "worker"}]},
  { name: "Louis", role: "supervisorB" }]  }

This is the function I wrote:

const users = [ 
  {name: "Anna", role: "worker"},
  { name: "Mary", role: "supervisorA" },
  { name: "Louis", role: "supervisorB" },  
  { name: "Saviole", role: "ceo" }
  ];

const recursiveAddToTree = (parent, child, grandChildren, users)=>{
  let tree = {};
users.forEach(( user)=>{
if(user.role===parent){
   tree ={...user}
} else if(user.role===child){
 tree = {...tree, chidren:[...[user]]}
} else {
 users.forEach(userChild=>{ 
   if(userChild.role===child){
   tree = {...tree, children:[...[{...userChild, chidren: [...[user]]}]]}
   }
 })
}
})
return tree;
}

const createSchema = users =>{
return recursiveAddToTree("ceo", "supervisor","worker", users)
}

Can you help to solve this issue? I don’t understand why it doesn’t working?

It seems you never call createSchema, thus when executing your code it actually never runs.

However, there is another problem, that when I call it, it returns me a single object {"name":"Saviole", "role":"ceo"} , so it seems that the recursiveAddToTree also still has a problem – but I guess you are on the right track, here.

The are no users with “supervisor” roles, and the final else statement can’t work as there are no supervisors (it isn’t corrent anyway). Also, minor typo, you’d be creating an object with a key chidren. Also the name is wrong: this is not a recursive function.

Main issue is that you haven’t defined what the heirarchy is: you have two different types of supervisor, how do you specify who works for whom? There is no link in the input data between these things, so you can’t write a program that abstracts this: you need to know up front what the structure is, the data does not tell you.

The data should really be like:

const users = [
  {
    name: "Anna",
    role: "worker",
    report: "supervisorA",
  },
  {
    name: "Mary",
    role: "supervisorA",
    report: "ceo",
  },
  {
    name: "Louis",
    role: "supervisorB"
    report: "ceo",
  },
  {
    name: "Saviole",
    role: "ceo",
    report: null,
  },
];

This looks more sane. You’ve kinda tried to this via the parameters to the function, but that won’t work unless a. the heirarchy is linear (which it isn’t – it’s a tree) and b. you keep adding roles, which will potentially make the function signature huge and extremely unwieldy. You need the input data to specify who they report to, you cannot infer it from what you have here, so it is not possible, logically, to write a function that builds this object for you automatically. Unless the data looks something like my example (ie the users have specified what links them), you can’t do what you’re trying to do. At the minute, you need the result you want from the function to be able to write the function in the first place.

1 Like

thank you, the problem is more clear now. I’ll try to do as you suggested

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.