2

I am using recursion to step through this array of object's children. However it is returning the top level parents. The array of objects is:

const orgs = {
   children:[{name:'Core Enginerinng Ops', orgId:741,
             children:[{name:'Child Engineering Ops', orgId:5656, 
                children:[{name: 'Child Engineering Last LEVEL AHAHHH', orgid:6969}]},{name: 'Child 2 Engineering OPS', orgId: 852}]},{name: 'Data Services Engineering', orgId: 456,
             children:[{name:'Child Data Services', orgId:978},{name: 'Child 2 Data Services', orgId: 354}]}]
    }

My end goal is to save the objects into a new array with just the name and the orgId as object for each parent and child.

flattenOrgs = (organizations) => {
  const flatArray =organizations.map(org => {
    if (org.children && org.children.length > 0) {
      this.flattenOrgs(org.children)
    }
    console.log(org.name)
    return org.name
  })
  return flatArray
}

However when I pass it through this function that uses recursion it only returns the 'org.name': ["Core Enginerinng Ops", "Data Services Engineering"]. I'm not great with recursion, but it doesn't make sense to me that the console.log(org.name) prints out each individual name as expected... But it doesn't return that name?

EDIT console.log(org.name) before return

Child Engineering Last LEVEL AHAHHH

Child Engineering Ops

Child 2 Engineering OPS

Core Enginerinng Ops

Child Data Services

Child 2 Data Services

Data Services Engineering

2 Answers 2

1

You could reduce the array instead of mapping, because you need the children elements as well.

const
    orgs = { children: [{ name: 'Core Enginerinng Ops', orgId: 741, children: [{ name: 'Child Engineering Ops', orgId: 5656, children: [{ name: 'Child Engineering Last LEVEL AHAHHH', orgid: 6969 }] }, { name: 'Child 2 Engineering OPS', orgId: 852 }] }, { name: 'Data Services Engineering', orgId: 456, children: [{ name: 'Child Data Services', orgId: 978 }, { name: 'Child 2 Data Services', orgId: 354 }] }] },
    flattenOrgs = (organizations) =>
        organizations.reduce((r, { name, orgId, children }) => 
            r.concat({ name, orgId }, flattenOrgs(children || [])), []);

console.log(flattenOrgs(orgs.children));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
0

I hope below code will meet your expectation:

const orgs = {
  children: [{
    name: 'Core Enginerinng Ops',
    orgId: 741,
    children: [{
      name: 'Child Engineering Ops',
      orgId: 5656,
      children: [{
        name: 'Child Engineering Last LEVEL AHAHHH',
        orgid: 6969
      }]
    }, {
      name: 'Child 2 Engineering OPS',
      orgId: 852
    }]
  }, {
    name: 'Data Services Engineering',
    orgId: 456,
    children: [{
      name: 'Child Data Services',
      orgId: 978
    }, {
      name: 'Child 2 Data Services',
      orgId: 354
    }]
  }]
}

console.log(orgs.children.map(child => {
  let newchild = {
    name: child.name,
    orgId: child.orgId
  }
  child.children && child.children.map(innerchild => {
    newchild.children = {
      name: innerchild.name,
      orgId: innerchild.orgId
    }
    innerchild.children && innerchild.children.map(innermostChild => {
      newchild.children.children = {
        name: innermostChild.name,
        orgId: innermostChild.orgId
      }
    })
  })
return newchild
}))

Comments

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.