Get each object in an array using recursion | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Get each object in an array using recursion

How can i add each object in this array of object https://code.sololearn.com/WHZR0s59Z5et/?ref=app

26th Jul 2020, 6:54 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
10 ответов
0
Btw thanks guy for answering my questions here's my solve problem abaout recursive https://code.sololearn.com/WZpWbJ5DjWHq/?ref=app
26th Jul 2020, 7:55 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
+ 4
Hafizd Jubaidir Recursion is great for traversing nested nodes as seen in your example. If this was a code written on a development team I manage, I would encourage the use of functional programming methods like reduce while avoiding side effects involving outer scoped variables. While this technique may be more advanced than where you are at the moment, it's not that difficult to learn. This allows for writing code that is easier to unit test in isolation resulting in cleaner, more focused, and maintainable code. I hope this perspective helps. https://code.sololearn.com/Wtimy6CHj8GP/?ref=app Here is the gist of the JS from my code: ---- const flatten = (acc, item) => { return Array.isArray(item) ? acc.concat(item) : Object.values(item) .reduce(flatten, acc) }; let result = flatten([], company) ----
28th Jul 2020, 5:05 AM
David Carroll
David Carroll - avatar
+ 2
Line 17, "let arrTampung = [];", should be outside of the function satukan, so that it does not reset to an empty array every time the function satukan is invoked at line 22. Move it to line 15 or any line above the function satukan.
26th Jul 2020, 7:06 PM
ODLNT
ODLNT - avatar
+ 2
Hafizd Jubaidir I can't answer the question about recursive techniques in used in real world projects, maybe David Carroll or any other experienced software engineer/supervisor can help you.
26th Jul 2020, 7:34 PM
ODLNT
ODLNT - avatar
+ 1
what do you want to push in the array? how u want to store the data inside the array? (example: [{name,age}, {name,age})]
26th Jul 2020, 7:05 PM
durian
durian - avatar
+ 1
There might be good use cases of recursion but for simpler tasks there are better alternatives. For a decrease-and-conquer situation like your code. Loops are good. You can just move the loop to outside the function. For divide-and-conquer tasks. It is better to actually use different functions for such things. Like this code of mine https://code.sololearn.com/WLVSDXYQV8Fq/ In my opinion, recursion is just a fancier implementation of GOTOs. Avoid them if you can.
27th Jul 2020, 9:48 AM
Ore
Ore - avatar
0
Lily Mea I need add every object in array into array tampung so it will be like this tampung = [{name : jack, salary : 1100},{name: lucy, salary : 200}]. So how can i do that using Recursive ? Thank you in advance
26th Jul 2020, 7:14 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
0
I want to ask you ODLNT and Lily Mea . What recursive technic will often be used in real project ?
26th Jul 2020, 7:18 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
0
Ore ah ok thanks for your advices
27th Jul 2020, 6:55 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
0
Hafizd Jubaidir Last time I checked your code looked very different. The variable names where not in English. Am I right or did I mistakenly checked some other codes somewhere?
28th Jul 2020, 5:54 AM
Ore
Ore - avatar