Flatten Nested Arrays with Recursion in JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024
  • 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!
    Code: repl.it/@jchia...

ความคิดเห็น • 31

  • @reactivicky
    @reactivicky ปีที่แล้ว +1

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

  • @Tuba_fashions
    @Tuba_fashions 9 หลายเดือนก่อน +1

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

    • @jchiatt4
      @jchiatt4  9 หลายเดือนก่อน

      Appreciate it!

  • @karolina5786
    @karolina5786 3 ปีที่แล้ว +7

    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.

  • @juanceja899
    @juanceja899 2 ปีที่แล้ว

    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!

  • @josieicaza7031
    @josieicaza7031 2 ปีที่แล้ว +1

    Great explanation, please keep making videos!! :D

  • @techabl
    @techabl 3 หลายเดือนก่อน

    Great example for Recursion Thx

  • @harunaoseni555
    @harunaoseni555 3 ปีที่แล้ว

    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!!

  • @samiamgrnegsnhamnonftercan2ham
    @samiamgrnegsnhamnonftercan2ham 5 หลายเดือนก่อน +1

    That was awesome, thx man!

  • @merunus8556
    @merunus8556 2 ปีที่แล้ว

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

  • @hliasfasilakhs6765
    @hliasfasilakhs6765 ปีที่แล้ว

    Amazing explanation m8. Thank you

  • @blissboxx
    @blissboxx 2 ปีที่แล้ว

    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!

  • @MrWardo2009
    @MrWardo2009 3 ปีที่แล้ว

    Thanks for making this video! Excellent explanation!

  • @jreezy6047
    @jreezy6047 2 ปีที่แล้ว

    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!

  • @tiffanyvalentina6773
    @tiffanyvalentina6773 3 ปีที่แล้ว

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

  • @athenachiarello3090
    @athenachiarello3090 2 ปีที่แล้ว

    awesome video 👌🏼

  • @MrRhysSir
    @MrRhysSir 3 ปีที่แล้ว

    Great explaination, thank you!

  • @vesnamohar4273
    @vesnamohar4273 ปีที่แล้ว

    Thanks!

  • @keithvaneaton5840
    @keithvaneaton5840 2 ปีที่แล้ว

    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!

  • @wonnapee
    @wonnapee 2 ปีที่แล้ว

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

  • @soniakaushal2190
    @soniakaushal2190 3 ปีที่แล้ว

    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?

  • @emmanuellmiqueletti7029
    @emmanuellmiqueletti7029 3 ปีที่แล้ว

    nice. Do you have more questions like this?

  • @luckyjones2409
    @luckyjones2409 3 ปีที่แล้ว

    You are amazing!

  • @adamswalks2385
    @adamswalks2385 4 ปีที่แล้ว

    Very helpful, thank you!

  • @solo7836
    @solo7836 2 ปีที่แล้ว

    awesome way ; dude 🚀

  • @mhartein
    @mhartein 4 ปีที่แล้ว

    thanks, your video really helped me

  • @JohnBartmannMusic
    @JohnBartmannMusic 2 ปีที่แล้ว

    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)?

  • @ashatron656
    @ashatron656 4 ปีที่แล้ว

    Super helpful 👍

  • @Sam-cz7ck
    @Sam-cz7ck 3 ปีที่แล้ว

    great video :D

  • @vytautasrevuckas
    @vytautasrevuckas 4 ปีที่แล้ว

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

  • @DanEMO592
    @DanEMO592 10 หลายเดือนก่อน

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