score:3

Accepted answer
.as-console-wrapper { max-height: 100% !important; top: 0; }

score:1

const sampleObj = { 
  home: true,
  products_edit: true,
  products_create: true,
  orders_delete: true,
  pages_category_create: true
}

const res = Object.keys(sampleObj).reduce((acc,cur)=>{
  const value = sampleObj[cur];
  const tree  = cur.split("_");
  const root  = tree.shift();
  
  if(!acc[root]){
    acc[root] = {};
    if(tree.length === 0){
       acc[root].status = "full"
       return acc;
    } else {
       acc[root].subOptions = {};
       acc[root].status = "limited";
    }
  }
  
  acc[root].subOptions[tree.shift()] = tree.reverse().reduce((acc,cur)=>{
      return {[cur]:acc}
  }, value);
  return acc;
  
}, {});

console.log(res);

Related Query

More Query from same tag