Flatten Nested Arrays with Recursion in JavaScript

preview_player
Показать описание
This is one of those questions you might see in an interview that actually has some real-world use. While you may end up just using a library to flatten an array out in the wild, it never hurts to know how to write your own!

Рекомендации по теме
Комментарии
Автор

This explanation is better than the solution. and it should be in every educational videos
hats off man

Sameer-codes
Автор

Why doesn't this video have more views? This is the 4th video on this topic that I've watched, and you're the ONLY one that actually takes time to explain each step in a way that can be understood. Thank you so much for this.

karolina
Автор

Great solution. Thank you. I got asked this in my interview yesterday.

reactivicky
Автор

I've watched a few videos on this same problem and I have to say, this is the best one yet. Great solution and great explanation. Thank you!

juanceja
Автор

Great explanation, please keep making videos!! :D

josieicaza
Автор

I'm late to the party, but keep on putting out videos my man, keep on, for recursion not being my strong suit, I understood this, thank you!!

harunaoseni
Автор

Great example of recursion, it was interesting, thank you.

merunus
Автор

saw this using the reduce method and it boggled my mind.. seeing the same thing done with a forloop has helped me digest it a lot better!

blissboxx
Автор

Thanks for making this video! Excellent explanation!

MrWardo
Автор

The array i was working with went a couple levels deeper, and I had to use the spread operator with concat to go all the way down. But the rest of this was so helpful! Thank you!

jreezy
Автор

Good tutorial. Thanks for the insight of For loops vs. For Each.

tiffanyvalentina
Автор

Awesome video! I recognize this isn't the same as flat or flatMap but it's still useful regardless

DanEMO
Автор

9:00 - I would just add that: concat returns NEW object so you need that equals on line 12

wonnapee
Автор

nice. Do you have more questions like this?

emmanuellmiqueletti
Автор

Hi J C, very good explanation, Thanks.

Could you help me understand the time complexity of this code?

I am thinking it should be N*K where N is the length of the main array and K is the length of the longest nested array. Is this correct?

soniakaushal
Автор

This is great, thank you! but could you please apply it to a real scenario? Currently if I apply this function to an existing nested array, no flattening takes place. Perhaps you have an example on the webs somewhere? Many Thanks for considering!

keithvaneaton
Автор

I don't understand why "let flattened = []" isn't resetting to an empty array each time the function is called recursively. Surely it would overwrite the previous values stored in "flattened" and you'd end with the result of "flattened = [9]" (ie the final array to pass the isArray test)?

JohnBartmannMusic
Автор

How to find the biggest number with recursion?
console.log( bigNum( [ 'asfd', -5, true, false, [7, 9], [1, 2, [8, 99, [87878, -22]]], {} ] ), '->', 87878 );

vytautasrevuckas