Array Reduce in 100 seconds

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

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

  • @RaviYadav-bt2eg
    @RaviYadav-bt2eg 4 ปีที่แล้ว +442

    Try *this* keyword in 100sec

    • @Fireship
      @Fireship  4 ปีที่แล้ว +123

      This is a good idea!

    • @danvilela
      @danvilela 4 ปีที่แล้ว +9

      wuth "bind" .. that func doesnt make sense to me

    • @Failizard
      @Failizard 4 ปีที่แล้ว +2

      Yes please

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

      @@danvilela 'this' is a placeholder 'bind' fills in that placeholder

    • @tomifiju
      @tomifiju 4 ปีที่แล้ว +4

      @@Fireship This was a pun?

  • @Oswee
    @Oswee 4 ปีที่แล้ว +97

    I love how you show the "vanilla" for loop in comparison. That gives complete context for reduce.

  • @juliohintze595
    @juliohintze595 4 ปีที่แล้ว +201

    If you omit the reduce's second argument, the loop will start at the second item, and the accumulator will be the value of the first item in the first iteration.
    This is useful if you are just adding the values in a number array. If you are trying to reduce an array of objects to a number, you shouldn't omit the second argument (specially if you are using TypeScript, that sets the .reduce() return type to the second argument's type or, if omitted, the array type)

    • @Fireship
      @Fireship  4 ปีที่แล้ว +43

      That is a good point, thank you!

    • @darxoonwasser
      @darxoonwasser 4 ปีที่แล้ว +4

      didn't know this, thanks

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

      I always add the second argument as a safety measure.

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

      Thanks for the info!

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

      how does this work? How do you return the accumulator if you don't have access to it

  • @rolikaseventysix
    @rolikaseventysix 4 ปีที่แล้ว +27

    I love this one. Short (no bullshit), but yet fully explanatory. Absolutely fits the needs of a newbie or an advanced programmer, who wants to refresh his knowledge. Thank you!

  • @oskrm
    @oskrm 4 ปีที่แล้ว +152

    Try Promises in 100sec

    • @Fireship
      @Fireship  4 ปีที่แล้ว +55

      Already have it recorded!

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

      @@Fireship show me bruda

    • @moseskarunia
      @moseskarunia 4 ปีที่แล้ว +22

      @@Fireship you should answer with "i promise i will!"

    • @neeeeeck9005
      @neeeeeck9005 4 ปีที่แล้ว +2

      Wow thats a long promise resolve

    • @Selur91
      @Selur91 4 ปีที่แล้ว +2

      @@Fireship If I could subscribe again I would, Promises have been a headache for me.

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

    fireship: explains the reduce method in depth in 100 seconds
    also fireship: tries to explain all of react in 100 seconds

  • @alekseysoldatenkov5675
    @alekseysoldatenkov5675 4 ปีที่แล้ว +3

    The true utility behind arr.reduce() is modifying different parts of the array based on the current value and/or index; not less typing vs. a for-loop.
    Because a for-loop can do all a basic arr.reduce() method can, plus it can be made faster by doing a reverse for loop.

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

      That is a fair point. I was not trying to imply reduce it is superior, just wanted to use it for comparison. In general, js for loops are much faster than array methods.

  • @aleksandarstevanovic5854
    @aleksandarstevanovic5854 4 ปีที่แล้ว +3

    You guys are reading my mind! I just starred replacing regular forEach loop with reduce wherever I can

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

    Starting to ask myself why I wasted hours of my life learning programming concepts only to discover your channel years later, explaining them better in 100 seconds each. Thanks for refreshing my memory on this and keep 'em coming!

  • @StephenMoreira
    @StephenMoreira 4 ปีที่แล้ว +4

    I am liking these in 100 seconds clips. Thanks.

  • @p.1206
    @p.1206 4 ปีที่แล้ว +4

    That helped me as a Java developer too thanks.

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

    Fireship is the type of channel whose video I'll Like before watching. Thanks!

  • @Noah-vm8id
    @Noah-vm8id 2 ปีที่แล้ว

    Reducers are so useful... once you got the concept you can do really great things with them

  • @transmissions1135
    @transmissions1135 4 ปีที่แล้ว +21

    ESModules in 100 seconds!!

  • @Nyasha_Nziboi
    @Nyasha_Nziboi 4 ปีที่แล้ว +3

    Thank you so much for this iv always struggled to understand reduce till now

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

    The visual of your videos is just exceptional!

  • @troythompson2
    @troythompson2 4 ปีที่แล้ว +12

    Love these lil commercials lol

  • @sergioguerrero3117
    @sergioguerrero3117 4 ปีที่แล้ว +4

    Estos videos me enseñan mas en 100 segundos que dos horas de clase en la universidad

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

    Thank you Fireship, for providing your VS Code theme and font in the video description!

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

    100 secondes videos are the best thing happend on youtube

  • @alberto.polini
    @alberto.polini 4 ปีที่แล้ว +3

    I likes this format, maybe i can apply It to my videos too.
    You are doing a great job😊

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

    My lord thank you for making this, I've always been confused about reduce. It turns out I didn't even know what it did!

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

    100 seconds of pure knowledge..!!

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

    As someone that uses js alot these are nice videos to help other people understand js, nice job :D

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

    This series is eye opening.

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

    Been working with js for a while and never really got the reduce method, thanks!

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

    This series is gold!

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

    I can't believe it's only been a few years that he's been making videos

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

    Filter Map Reduce (in that order) are the most common operation. (Reduce is a bad name, Sum is more appropriate.)
    To solve "what's the total salary of local viewers?" you just
    allViewers.Filter( for local ).Map( to v.salary ).Reduce( sum )

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

    Excellent description. Thank you.

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

    Really loving these short episodes as fillers. Cheers Jeff 🙏🏻

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

    0:52 haha! I agree `accumulator` is a fancy word too !

  • @ಠ_ಠ-ಲ6ಬ
    @ಠ_ಠ-ಲ6ಬ 4 ปีที่แล้ว +1

    Yea... I'm just gonna stick with my for loop for now :) Why change something that already works and pretty much totally explains itself.

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

    This is very helpful
    Thank you really for this short this clear explanation

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

    personally i wouldn't choose reduce() over simple loop for such use cases

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

    Great now I've to change my shopping cart function but I like better 😁 thanks Jeff 🤓 these short tips are useful 👌

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

    ReduceRight..... Wow. never heard of it

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

    I'd love to learn about your console setup. I like it outputs right in there.

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

      he uses QuokkaJS I think
      It displays result inline in vs code.

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

    So this is where it all began!

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

    which theme you are using in VS code..??? By the I loved the video..!

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

    Really enjoying these, excited for more 👍

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

    FOR THE LOVE OF SATAN please make more of these type of videos I NEEEEDDDD ITTTTTT

  • @khotambakhromov
    @khotambakhromov 4 ปีที่แล้ว +6

    let's do JS in 100 seconds :D

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

      Lets do whole web develoment in 100 secs

  •  4 ปีที่แล้ว

    What a good explanation. Thank you so much!

  • @renion9547
    @renion9547 4 ปีที่แล้ว +9

    Try RxJs in 100sec

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

      I have a few RxJS videos recorded

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

    For the order total problem, which would you go for?
    a)
    . const total = orders.map(o => o.total).reduce((a, b) => a + b);
    b)
    . const total = orders.reduce((acc, o) => acc + o.total);

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

      the first iterates over the array twice,
      I don't know why it wasn't mentioned but the significant power of reduce comes from it's ability to combine many maps into one iteration

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

      @@FluffehTheSheep exactly, this looks useful at first glance, but it's actually twice as slow

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

    Love this format!

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

    Cool ! I was wondering if the result of the console.log that prints in the rightside is an edit or an extension for VSCode

    • @Fireship
      @Fireship  4 ปีที่แล้ว +5

      Quokka plugin

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

    When comparing it with a for loop, you say the difference is reduce() doesn't mutate the app state directly. I fail to understand exactly what you mean. If it mutating the state indirectly? And what are the benefits or use cases for doing that?

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

      It means that you don't change the thing you put into the function. Instead, you create a new thing (an integer) which you use for the rest of the computation. This prevents you from overwriting the original array which you may not want.

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

    Hi Jeff, how long will the sale for the pro membership be on for?

  • @Asiro-S
    @Asiro-S 4 ปีที่แล้ว

    Thank you, very informative.

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

    PLEASE tell me the name of the song! I know it from somewhere and I've been looking for it for MONTHS! Please tell

    • @5dba4
      @5dba4 ปีที่แล้ว

      Hi there, have you found it yet?

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

    Lodash vs native JS functions. What is your opinion and can you make a video on that?

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

    I am liking this 100s 👌

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

    Wow very effective video !!

  • @rafik.arif.83
    @rafik.arif.83 3 ปีที่แล้ว

    of course i will see u in the next video.

  • @Yash-fz7kw
    @Yash-fz7kw 2 ปีที่แล้ว +1

    Thanks

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

    Hi, could you share please what software do you use for: video edit, animations, graphic? Thx.
    PS: I mean just for this video.

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

    Thanks for the video. Any chance you could remove the background music please? It makes it harder to hear and process what you're saying. Unlike everything else in your videos, it doesn't seem to serve a purpose.

    • @Fireship
      @Fireship  4 ปีที่แล้ว +5

      I hear you, that's a bit of a divisive issue. I might put up a poll soon to decide.

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

      How about open the auto-generated subtitle? It 's very accurate. Without that drive-in-space like background music, Fireship will not be Fireship. 🤯

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

      @@Fireship I have been looking for this background music for years. I cannot find it. I love it, it just strikes something within me. Please, Fireship, if you read this, please tell me what is that music called.

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

    More please :)

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

    JS generators next please :)

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

    This is so nice , thank you.

  • @AhmadHassan-ss5un
    @AhmadHassan-ss5un 4 ปีที่แล้ว +2

    make more 100 seconds videos

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

    Nice format.

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

    @fireship - is reduceRight faster than reduce similar to negative for loops being faster (due to how memory works)

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

    Which is readable and simple? The for-loop. Use the for-loop.

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

    These are great.

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

    I like this format

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

      Good to know, many more on the way :)

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

      @@Fireship nice :)

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

    Rule of searching for good tutorials.
    Check if the video was posted by Fireship or Traversy Media :D

  •  4 ปีที่แล้ว

    Thankyou!

  • @energy-tunes
    @energy-tunes 2 ปีที่แล้ว

    Did you start shift pitching your newer videos lol

  • @DeAndreJohnson212
    @DeAndreJohnson212 4 ปีที่แล้ว +3

    Do Recursion in 100sec

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

      Recursion is next in line

  • @etymology_
    @etymology_ 4 ปีที่แล้ว +2

    you should do 200 seconds so I can comprehend what is actually going on in the video...

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

    bricks are apparently more expensive

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

    does JavaScript not have a sum function for arrays like Python does for lists?

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

    Is this just about the same thing as foldl and foldr in Haskell?

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

    Why did you use currentOrder.total instead of just currentOrder?
    What does `total` refer to?
    Is it a reference to total as a property of PaymentRequestEvent from PaymentRequest API?
    That's the only thing I'm gettings as result of my googling and it sort of makes sense...
    @edit
    Watched the vid once again and I immediately understood that: „hey, we're working on array of objects and this is how to access the value of certain property in object”
    Damn, sometimes not noticing such things is really silly :/

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

    explain reduce in the shortest time :)

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

    I finally understand

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

    Mejor que los videos de 30min

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

    Amazing.

  • @Скыбыдыщь
    @Скыбыдыщь 4 ปีที่แล้ว

    So cool

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

    Doing a quick google search shows that the reduce approach performs slower than the simple loop approach on many occasions? Any comment on this?

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

      it is slower, I talk about it in this vid th-cam.com/video/x7Xzvm0iLCI/w-d-xo.html

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

    amazing 🔥

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

    It is like producing items

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

    Suppose I have 2 keys Amount and Total and i want sum to both columns so how i can i write this in reduce functions?

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

    even the ads are longer than the video!

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

    not me needing to put it on 0.75% because im slow af 😂

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

    So basically reduce is more work (more statements) and more difficult to understand? The first programmer must learn this + implement it and all the others maintaining the code must undstand what is going on while the plain for-loop would to the same and be self-explanatory with only basic instructions?

    • @AndreiGeorgescu-j9p
      @AndreiGeorgescu-j9p 2 หลายเดือนก่อน

      You shouldn't legally be allowed to program

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

    Is this less efficient than a for loop

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

    vscode color theme ??

  • @Philson
    @Philson 4 ปีที่แล้ว +9

    Oh... It looks a little bit like recursion.

    • @Fireship
      @Fireship  4 ปีที่แล้ว +9

      And that just so happens to be the next topic on the list

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

      you can implement it using recursion :)

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

    and what are the benefits of array reduce?

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

    11:11 - Hoezaay..

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

    Try sorting / filter array in 100sec

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

    Is there a call/cc in js to escape the reduce function?

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

      Not really. You could throw an error. A regular for loop is probably better as it can use `continue` or `break`.

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

      Technically you can modify the array to be shorter (you may wish to start witha copy of the array)

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

    does anyone know the extension for the emoji's he uses?

  • @Alex-pd5xv
    @Alex-pd5xv 4 ปีที่แล้ว

    Try multi-threading in 100 seconds

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

    🔝

  • @kamalhm-dev
    @kamalhm-dev 4 ปีที่แล้ว

    reduceRight should've been called reduceLeft