+ 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
10 Answers
0
Btw thanks guy for answering my questions here's my solve problem abaout recursive
https://code.sololearn.com/WZpWbJ5DjWHq/?ref=app
+ 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)
----
+ 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.
+ 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.
+ 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})]
+ 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.
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
0
Ore ah ok thanks for your advices
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?