0

I have an array like this.

var main_childs = [
    {child_name: 1, childs:[2,4]},
    {child_name: 2, childs:[3]},
    {child_name: 3, childs:""},
    {child_name: 4, childs:""}
]

main_child.childname 1 have childs 2 and 4 that exist in main_childs so if main_childs.child_name is a child of one of the child name it will output like this

final_childs = [
    {
        1: {
            name: 1,
            childs: [
                {
                    name: 2, 
                    childs: [
                            {
                                name: 3, 
                                childs: []
                            },

                        ],
                    name: 4,
                    childs:[]

                }
            ]
        },

    }
]

I'm trying to solve it using this function

  function recursive(main_childs){
   main_childs.forEach(i=> {
       if(i.childs.length>0){
         recursive(i.childs)
       }
  })

Any help will be appreciated

6
  • 1
    You've forgot to show us your attempts (even pseudo code) to solve your issue yourself Commented Aug 20, 2021 at 7:55
  • You are trying to build a tree data structure - is child 1 guaranteed to be the root node of the tree? Commented Aug 20, 2021 at 7:56
  • yes sir @Taxel I'm trying to solve it using recursive function but I can't solve it Commented Aug 20, 2021 at 7:58
  • I believe recursion will make this problem a lot harder than it needs to be. Commented Aug 20, 2021 at 8:12
  • @Ouroborus Can u recommend me any solution to solve this sir ? Commented Aug 20, 2021 at 8:14

2 Answers 2

1

Assuming 1 is not always root but other elements can also be root and elements can be out of order as well added extra element

{ child_name: 5, childs: [2] }

Wrote it hastily so ignore function naming, output looks weird but if you tidy it with tabs its the same below code does not handle cyclic dependencies

   var main_childs = [{
    child_name: 1,
    childs: [2, 4]
  },
  {
    child_name: 2,
    childs: [3]
  },
  {
    child_name: 3,
    childs: ""
  },
  {
    child_name: 4,
    childs: ""
  },
  {
    child_name: 5,
    childs: [2]
  }
]

//get all roots

const getRoots = () => {

  let allChilds = main_childs.map(x => {
    if (!x.childs) {
      x.childs = []
    }
    return [...x.childs]
  }).flat()

  let allRoots = main_childs.filter(x => !allChilds.find(y => x.child_name == y)


  )

  return allRoots


}

//recursive call
const transform = (obj) => {
  let result = {}
  result.name = obj.child_name
  result.childs = []
  if (obj.childs) {
    obj.childs.forEach(x => {
      let n = main_childs.find(y => y.child_name == x)
      if (n) {
        result.childs.push(transform(n))
      }
    })

  }
  return result
}
//creation of new structure
const createNewStructure = () => {
  let roots = getRoots()
  let result = []

  roots.forEach(x => {
    let n = {}
    n[x.child_name] = transform(x)
    result.push(n)
  })

  return result
}


console.log(createNewStructure())


//output
/* [{
  1: {
    childs: [{
  childs: [{
  childs: [],
  name: 3
}],
  name: 2
}, {
  childs: [],
  name: 4
}],
    name: 1
  }
}, {
  5: {
    childs: [{
  childs: [{
  childs: [],
  name: 3
}],
  name: 2
}],
    name: 5
  }
}] */
Sign up to request clarification or add additional context in comments.

Comments

1

EDIT: I have add a function that search all parents

Try this:

var main_childs = [
    {child_name: 1, childs:[2,4]},
    {child_name: 2, childs:[3]},
    {child_name: 3, childs:""},
    {child_name: 4, childs:""},
    {child_name: 5, childs:[]}
]

var final_childs=[]

var parents=getParents();

parents.forEach(child=>{
    var parent={name:child.child_name,childs:[]};
  recursiveFunc(parent,child.childs);
  final_childs.push({});
  final_childs[final_childs.length-1][parent.name]=parent;
}

)
console.log(final_childs);

function recursiveFunc(parent,childNames){
  if(childNames=="")
    return
  childNames.forEach(childName=>{
    var child={name:childName,childs:[]}
    var main_child=main_childs.find(el=>el.child_name==childName);
    recursiveFunc(child,main_child.childs)
    parent.childs.push(child)
  })
}


function getParents(){
  let childs=[];
  let parents=[];
  main_childs.forEach(child=>childs.push(child.childs));
  childs=childs.flat(2);
  main_childs.forEach(child=>{
    let name=childs.find(el=>child.child_name==el);
    if(name==null){
      parents.push(child);
    }
  });
  return parents; 
}

1 Comment

thank you for the answer but when I added new object {child_name: 5, childs:[]} then the final_child must have 2 entries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.