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