+ 3
After some hours of trying, I got duplicated results! I don't know where the problem lies. Please help.
Recursively adding names in deep nested object https://code.sololearn.com/ca10a9A19a11/?ref=app
6 Respuestas
+ 2
because you add 'name' property how many times there's keybwich are not array ('name' and 'value')...
to get only names, don't iterate keys, just do:
function addNames(tree){
return tree.name+tree.children.reduce((a,o) => a+addNames(o),"")
}
if you are not sure that 'children' contains an array and/or children is defined:
function addNames(tree){
return tree.name+(tree.children && Array.isArray(tree.children) ? tree.children.reduce((a,o) => a+addNames(o),"") : "")
}
+ 3
visph I'm here again good friend. I got duplicated results. I don't know why. So close
+ 3
visph aha I seee. I get it now! I was so close. Thanks bro ☺️ ☺️ ☺️ ☺️ ☺️
+ 3
visph i think I've noticed a pattern in approaching such problems. First, finding the simplest form of the problem and then writing code to solve that but when there's a difference in the data type or structure, an if statement is used together with the function itself to cater for it. That's what I realized. I'll apply this method to the other questions ☺️☺️☺️. Thanks @visph
+ 3
visph aha cool 😎 😎 😎
+ 1
maybe: I must confess that my mind / brain works usually more well with the intuitive / instinctive approach ;P