Reduce each node of tree into list of tips of tree
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
add a comment |
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
add a comment |
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
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
javascript node.js tree
edited Nov 25 '18 at 6:24
rakim
asked Nov 25 '18 at 6:18
rakimrakim
403211
403211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 7:40
rakimrakim
403211
403211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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