Map return returning last iteration of recursion?












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











share|improve this question





























    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











    share|improve this question



























      2












      2








      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











      share|improve this question















      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








      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 13:48

























      asked Nov 19 '18 at 13:42









      user10204157

      1339




      1339
























          2 Answers
          2






          active

          oldest

          votes


















          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; }








          share|improve this answer























          • Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
            – user10204157
            Nov 19 '18 at 14:03





















          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
          }))








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375929%2fmap-return-returning-last-iteration-of-recursion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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; }








            share|improve this answer























            • Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
              – user10204157
              Nov 19 '18 at 14:03


















            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; }








            share|improve this answer























            • Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
              – user10204157
              Nov 19 '18 at 14:03
















            1












            1








            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; }








            share|improve this answer














            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; }








            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; }





            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; }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 13:59

























            answered Nov 19 '18 at 13:53









            Nina Scholz

            176k1389155




            176k1389155












            • Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
              – user10204157
              Nov 19 '18 at 14:03




















            • Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
              – user10204157
              Nov 19 '18 at 14:03


















            Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
            – user10204157
            Nov 19 '18 at 14:03






            Nina....I feel like you just showed me THE cheat code with .reduce. I've never heard of it before...Thank you
            – user10204157
            Nov 19 '18 at 14:03















            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
            }))








            share|improve this answer


























              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
              }))








              share|improve this answer
























                0












                0








                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
                }))








                share|improve this answer












                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
                }))








                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
                }))





                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
                }))






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 13:59









                Arulmozhi Manikandan

                1665




                1665






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375929%2fmap-return-returning-last-iteration-of-recursion%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules