Reduce each node of tree into list of tips of tree












1















Here is the runnable code:
https://gist.github.com/the1mills/61d53438a3dce1da32640d3e05a611a6



(I couldn't figure out how to load the async library using JSBin or RequireBin online, maybe someone knows how to do that).



I have this tree structure:



const animals = {
canines: {
dogs: {
poodle: {
val: true
}
},
fox:{
val: true
},
wolf: {
northwestern:{
val: true
},
arctic: {
val: true
}
},
raccoon:{
val: true
}
},
porpoises: {
vaquita:{
val: true
},
harbor: {
val: true
}
},

};


For each node in the tree, I want to get a description of each branch from that node, reducing the branch keys into one key, so that I get:



  // canines node: 
[{"Dogs.Poodle": true}, {"Fox":true}, {"Wolf.Northwestern":true}, {"Wolf.Arctic":true}, {"Raccoon" : true}]

// porpoisies node:
[{"Vaquita": true}, {"Harbor":true}]

// and at the animals node:
[{"Canines.Dogs.Poodle": true}, {"Canines.Fox":true}, {"Canines.Wolf.Northwestern":true}, {"Canines.Wolf.Arctic":true}, {"Canines.Raccoon" : true}, {"Porpoises.Vaquita": true}, {"Porpoises.Harbor":true}]


I have this code, but I can't figure out what's wrong with it. I need to keep it asynchronous because I will be doing so I/O, but we can simulate that with process.nextTick for the purposes of the question.



const uppercaseFirstChar = s => {
return s.slice(0,1).toUpperCase() + s.slice(1).toLowerCase();
};


const loop = (v, list, cb) => {

const results = ;

async.eachLimit(Object.keys(v), 3, (k, cb) => {

const sub = v[k];

if (sub && typeof sub === 'object') {

for (let l of list) {
l.push({
key: k
});
}

return loop(sub, list.concat([results]), err => {

const path = results.reduce((a, b) => {
return {
val: a.val,
key: uppercaseFirstChar(a.key) + '.' + uppercaseFirstChar(b.key)
}
});

console.log({path});

cb(err);
});
}

for (let l of list) {
l.push({
val: sub,
key: k
});
}

process.nextTick(cb);


}, cb);

};

const list = ;

loop(animals, list, (err, val) => {
console.log(err, val);
});


In my code, for each node I am looking the tree paths, and I am getting some wild results, I cannot figure out why.










share|improve this question





























    1















    Here is the runnable code:
    https://gist.github.com/the1mills/61d53438a3dce1da32640d3e05a611a6



    (I couldn't figure out how to load the async library using JSBin or RequireBin online, maybe someone knows how to do that).



    I have this tree structure:



    const animals = {
    canines: {
    dogs: {
    poodle: {
    val: true
    }
    },
    fox:{
    val: true
    },
    wolf: {
    northwestern:{
    val: true
    },
    arctic: {
    val: true
    }
    },
    raccoon:{
    val: true
    }
    },
    porpoises: {
    vaquita:{
    val: true
    },
    harbor: {
    val: true
    }
    },

    };


    For each node in the tree, I want to get a description of each branch from that node, reducing the branch keys into one key, so that I get:



      // canines node: 
    [{"Dogs.Poodle": true}, {"Fox":true}, {"Wolf.Northwestern":true}, {"Wolf.Arctic":true}, {"Raccoon" : true}]

    // porpoisies node:
    [{"Vaquita": true}, {"Harbor":true}]

    // and at the animals node:
    [{"Canines.Dogs.Poodle": true}, {"Canines.Fox":true}, {"Canines.Wolf.Northwestern":true}, {"Canines.Wolf.Arctic":true}, {"Canines.Raccoon" : true}, {"Porpoises.Vaquita": true}, {"Porpoises.Harbor":true}]


    I have this code, but I can't figure out what's wrong with it. I need to keep it asynchronous because I will be doing so I/O, but we can simulate that with process.nextTick for the purposes of the question.



    const uppercaseFirstChar = s => {
    return s.slice(0,1).toUpperCase() + s.slice(1).toLowerCase();
    };


    const loop = (v, list, cb) => {

    const results = ;

    async.eachLimit(Object.keys(v), 3, (k, cb) => {

    const sub = v[k];

    if (sub && typeof sub === 'object') {

    for (let l of list) {
    l.push({
    key: k
    });
    }

    return loop(sub, list.concat([results]), err => {

    const path = results.reduce((a, b) => {
    return {
    val: a.val,
    key: uppercaseFirstChar(a.key) + '.' + uppercaseFirstChar(b.key)
    }
    });

    console.log({path});

    cb(err);
    });
    }

    for (let l of list) {
    l.push({
    val: sub,
    key: k
    });
    }

    process.nextTick(cb);


    }, cb);

    };

    const list = ;

    loop(animals, list, (err, val) => {
    console.log(err, val);
    });


    In my code, for each node I am looking the tree paths, and I am getting some wild results, I cannot figure out why.










    share|improve this question



























      1












      1








      1


      2






      Here is the runnable code:
      https://gist.github.com/the1mills/61d53438a3dce1da32640d3e05a611a6



      (I couldn't figure out how to load the async library using JSBin or RequireBin online, maybe someone knows how to do that).



      I have this tree structure:



      const animals = {
      canines: {
      dogs: {
      poodle: {
      val: true
      }
      },
      fox:{
      val: true
      },
      wolf: {
      northwestern:{
      val: true
      },
      arctic: {
      val: true
      }
      },
      raccoon:{
      val: true
      }
      },
      porpoises: {
      vaquita:{
      val: true
      },
      harbor: {
      val: true
      }
      },

      };


      For each node in the tree, I want to get a description of each branch from that node, reducing the branch keys into one key, so that I get:



        // canines node: 
      [{"Dogs.Poodle": true}, {"Fox":true}, {"Wolf.Northwestern":true}, {"Wolf.Arctic":true}, {"Raccoon" : true}]

      // porpoisies node:
      [{"Vaquita": true}, {"Harbor":true}]

      // and at the animals node:
      [{"Canines.Dogs.Poodle": true}, {"Canines.Fox":true}, {"Canines.Wolf.Northwestern":true}, {"Canines.Wolf.Arctic":true}, {"Canines.Raccoon" : true}, {"Porpoises.Vaquita": true}, {"Porpoises.Harbor":true}]


      I have this code, but I can't figure out what's wrong with it. I need to keep it asynchronous because I will be doing so I/O, but we can simulate that with process.nextTick for the purposes of the question.



      const uppercaseFirstChar = s => {
      return s.slice(0,1).toUpperCase() + s.slice(1).toLowerCase();
      };


      const loop = (v, list, cb) => {

      const results = ;

      async.eachLimit(Object.keys(v), 3, (k, cb) => {

      const sub = v[k];

      if (sub && typeof sub === 'object') {

      for (let l of list) {
      l.push({
      key: k
      });
      }

      return loop(sub, list.concat([results]), err => {

      const path = results.reduce((a, b) => {
      return {
      val: a.val,
      key: uppercaseFirstChar(a.key) + '.' + uppercaseFirstChar(b.key)
      }
      });

      console.log({path});

      cb(err);
      });
      }

      for (let l of list) {
      l.push({
      val: sub,
      key: k
      });
      }

      process.nextTick(cb);


      }, cb);

      };

      const list = ;

      loop(animals, list, (err, val) => {
      console.log(err, val);
      });


      In my code, for each node I am looking the tree paths, and I am getting some wild results, I cannot figure out why.










      share|improve this question
















      Here is the runnable code:
      https://gist.github.com/the1mills/61d53438a3dce1da32640d3e05a611a6



      (I couldn't figure out how to load the async library using JSBin or RequireBin online, maybe someone knows how to do that).



      I have this tree structure:



      const animals = {
      canines: {
      dogs: {
      poodle: {
      val: true
      }
      },
      fox:{
      val: true
      },
      wolf: {
      northwestern:{
      val: true
      },
      arctic: {
      val: true
      }
      },
      raccoon:{
      val: true
      }
      },
      porpoises: {
      vaquita:{
      val: true
      },
      harbor: {
      val: true
      }
      },

      };


      For each node in the tree, I want to get a description of each branch from that node, reducing the branch keys into one key, so that I get:



        // canines node: 
      [{"Dogs.Poodle": true}, {"Fox":true}, {"Wolf.Northwestern":true}, {"Wolf.Arctic":true}, {"Raccoon" : true}]

      // porpoisies node:
      [{"Vaquita": true}, {"Harbor":true}]

      // and at the animals node:
      [{"Canines.Dogs.Poodle": true}, {"Canines.Fox":true}, {"Canines.Wolf.Northwestern":true}, {"Canines.Wolf.Arctic":true}, {"Canines.Raccoon" : true}, {"Porpoises.Vaquita": true}, {"Porpoises.Harbor":true}]


      I have this code, but I can't figure out what's wrong with it. I need to keep it asynchronous because I will be doing so I/O, but we can simulate that with process.nextTick for the purposes of the question.



      const uppercaseFirstChar = s => {
      return s.slice(0,1).toUpperCase() + s.slice(1).toLowerCase();
      };


      const loop = (v, list, cb) => {

      const results = ;

      async.eachLimit(Object.keys(v), 3, (k, cb) => {

      const sub = v[k];

      if (sub && typeof sub === 'object') {

      for (let l of list) {
      l.push({
      key: k
      });
      }

      return loop(sub, list.concat([results]), err => {

      const path = results.reduce((a, b) => {
      return {
      val: a.val,
      key: uppercaseFirstChar(a.key) + '.' + uppercaseFirstChar(b.key)
      }
      });

      console.log({path});

      cb(err);
      });
      }

      for (let l of list) {
      l.push({
      val: sub,
      key: k
      });
      }

      process.nextTick(cb);


      }, cb);

      };

      const list = ;

      loop(animals, list, (err, val) => {
      console.log(err, val);
      });


      In my code, for each node I am looking the tree paths, and I am getting some wild results, I cannot figure out why.







      javascript node.js tree






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 6:24







      rakim

















      asked Nov 25 '18 at 6:18









      rakimrakim

      403211




      403211
























          1 Answer
          1






          active

          oldest

          votes


















          1














          When the code condenses/collapses itself, you usually know you're on the right track:



          const loop = (v, cb) => {

          const results = ;

          async.eachLimit(Object.keys(v), 3, (k, cb) => {

          const sub = v[k];

          if (sub && typeof sub === 'object') {

          return loop(sub, (err, values) => {

          for(let v of values){
          results.push(k + v);
          }

          cb(err);
          });
          }


          results.push(k);
          process.nextTick(cb);


          }, err => {
          cb(err, results);
          });

          };


          loop(animals, (err, val) => {
          console.log(err, val);
          });


          the problem with my code above was that object references in the array were being shared by all branches, so each branch was getting overpopulated, if you will.






          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%2f53465148%2freduce-each-node-of-tree-into-list-of-tips-of-tree%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            When the code condenses/collapses itself, you usually know you're on the right track:



            const loop = (v, cb) => {

            const results = ;

            async.eachLimit(Object.keys(v), 3, (k, cb) => {

            const sub = v[k];

            if (sub && typeof sub === 'object') {

            return loop(sub, (err, values) => {

            for(let v of values){
            results.push(k + v);
            }

            cb(err);
            });
            }


            results.push(k);
            process.nextTick(cb);


            }, err => {
            cb(err, results);
            });

            };


            loop(animals, (err, val) => {
            console.log(err, val);
            });


            the problem with my code above was that object references in the array were being shared by all branches, so each branch was getting overpopulated, if you will.






            share|improve this answer




























              1














              When the code condenses/collapses itself, you usually know you're on the right track:



              const loop = (v, cb) => {

              const results = ;

              async.eachLimit(Object.keys(v), 3, (k, cb) => {

              const sub = v[k];

              if (sub && typeof sub === 'object') {

              return loop(sub, (err, values) => {

              for(let v of values){
              results.push(k + v);
              }

              cb(err);
              });
              }


              results.push(k);
              process.nextTick(cb);


              }, err => {
              cb(err, results);
              });

              };


              loop(animals, (err, val) => {
              console.log(err, val);
              });


              the problem with my code above was that object references in the array were being shared by all branches, so each branch was getting overpopulated, if you will.






              share|improve this answer


























                1












                1








                1







                When the code condenses/collapses itself, you usually know you're on the right track:



                const loop = (v, cb) => {

                const results = ;

                async.eachLimit(Object.keys(v), 3, (k, cb) => {

                const sub = v[k];

                if (sub && typeof sub === 'object') {

                return loop(sub, (err, values) => {

                for(let v of values){
                results.push(k + v);
                }

                cb(err);
                });
                }


                results.push(k);
                process.nextTick(cb);


                }, err => {
                cb(err, results);
                });

                };


                loop(animals, (err, val) => {
                console.log(err, val);
                });


                the problem with my code above was that object references in the array were being shared by all branches, so each branch was getting overpopulated, if you will.






                share|improve this answer













                When the code condenses/collapses itself, you usually know you're on the right track:



                const loop = (v, cb) => {

                const results = ;

                async.eachLimit(Object.keys(v), 3, (k, cb) => {

                const sub = v[k];

                if (sub && typeof sub === 'object') {

                return loop(sub, (err, values) => {

                for(let v of values){
                results.push(k + v);
                }

                cb(err);
                });
                }


                results.push(k);
                process.nextTick(cb);


                }, err => {
                cb(err, results);
                });

                };


                loop(animals, (err, val) => {
                console.log(err, val);
                });


                the problem with my code above was that object references in the array were being shared by all branches, so each branch was getting overpopulated, if you will.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 7:40









                rakimrakim

                403211




                403211
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465148%2freduce-each-node-of-tree-into-list-of-tips-of-tree%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

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga