Reduce: 10 Different Examples. JavaScript Fundamentals.

Поділитися
Вставка
  • Опубліковано 22 жов 2024

КОМЕНТАРІ • 138

  • @ericzorn3735
    @ericzorn3735 4 роки тому +32

    For anyone learning JavaScript I find this absolutely imperative. Amazing video!

    • @leighhalliday
      @leighhalliday  4 роки тому +2

      Thank you very much, Eric! Glad you enjoyed it :)

    • @prinzadamz
      @prinzadamz 8 місяців тому

      True ✌

  • @metasavagex
    @metasavagex 4 роки тому +23

    This is why you're awesome, definitely showing the power of reduce, other tutorials just play around with it with adding values.
    Thank you for this 🔥 content!

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      Thanks James! Glad you found it useful!

  • @ayissinkejoelnarcisse9185
    @ayissinkejoelnarcisse9185 4 місяці тому

    Great video. Thanks a lot Mr Leigh 🙏

  • @danielluna7648
    @danielluna7648 3 роки тому +6

    This could be the single most helpful video I've seen all year. I had no idea how powerful reduce was. Get the concept of the accumulator down and it makes so many difficult tasks much easier. Great video. I would love to see more of these on other object and array functions. This was 🔥🔥🔥🔥🔥🔥

  • @adibirta
    @adibirta 3 роки тому +6

    20:23 These kind of moments are priceless :)) Great video! Thanks!

  • @newmagicfilms
    @newmagicfilms 3 роки тому +1

    Finally.... after all these years as a developer, I clearly understand it! Thank you!

  • @PavanSibal
    @PavanSibal 9 місяців тому

    The best reduce tutorial. Thank you.

  • @MicheleBloodphd
    @MicheleBloodphd 2 роки тому

    If you are struggling to wrap your head around JavaScript's REDUCE method, this is the video you want to watch. No need to look any farther. THIS IS THE ONE!

  • @mrkhoros
    @mrkhoros 4 роки тому +3

    Your dominance over arrays and objects is incredible. Please put more videos about these simple methods with these types of usages. Thanks.

    • @leighhalliday
      @leighhalliday  4 роки тому

      Haha... thanks :D But there's no need for anything else, only reduce!! Haha. Stay tuned for more vids in the future ;)

  • @piyushaggarwal5207
    @piyushaggarwal5207 2 роки тому

    Reduce is really cool man. Fun fun function guy told us that reduce can do map, filter and everything but didn't tell how. Saving your video for watching in detail later.

  • @alasdair7196
    @alasdair7196 2 роки тому

    BROOOOOO!! I have never understood how to use reduce, like at all...I do now. Thank you so much. Liked, saved and subscribed!

  • @ianfinley3012
    @ianfinley3012 2 роки тому

    Best description I have seen opening this up. Worlds ahead of anything else out there. Thank you!

  • @lucasae4140
    @lucasae4140 2 роки тому

    This is the first reduce tutorial I found that actually shows the value and versatility of reduce. hats off to you. I've been studying the methods shown in this video and I think my brain has bled at least a full gallon. this is such good and difficult study material. Thank you!

  • @thydevdom
    @thydevdom 3 роки тому

    THANK YOU THANK YOU THANK YOU! My instructor kept telling me that my code was fine but needed to implement reduce and shorten my code. You are a life saver. No other video made sense.

  • @0xmarrakesh288
    @0xmarrakesh288 2 роки тому

    Thank you a lot, friend! It was getting hard to wrap my head around the concenpt but now I finally get it! :)

  • @MakdoKro
    @MakdoKro 3 роки тому

    Big ty !!! thats the 1st video about reduce() which gives a good full explanation !

  • @viqtorione
    @viqtorione Рік тому

    Wonderful. Crystal clear. Thank you.

  • @mrWorldwideJuan
    @mrWorldwideJuan 2 роки тому +1

    For anyone learning JS right now, this is a must-watch. Thanks for sharing it!

  • @eraldoabedini4642
    @eraldoabedini4642 2 роки тому

    One of the best videos so far

  • @FromTheHeart-777
    @FromTheHeart-777 3 роки тому +1

    Omgggg ty. Everyone else's tutorials just have a set of numbers in their example. Your tutorial is more realistic on what is common. Thanks so much for having the intuition to have this tutorial. Subscribed!!

    • @leighhalliday
      @leighhalliday  3 роки тому +1

      Haha... thanks :) Glad you enjoyed it! I try to use realistic examples whenever possible.

    • @Rob34570
      @Rob34570 3 роки тому +1

      Me too, instant sub!
      Thanks for the realistic examples, exactly what I was looking for

  • @21agdmnm
    @21agdmnm 2 роки тому

    every freaking reduce video on youtube use the same example.. sum of some numbers.. FINALLY .reduce explained well!

  • @dimimav4418
    @dimimav4418 4 роки тому +4

    Hey Leigh thank you for the video. I was a little confused with the all and any over 18 implementations but i will watch again and try to wrap my head around!

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      Thanks Jim! Maybe I just didn't explain it very well :D Play around with it on your own and try to come up with some examples.. that's how you'll get more comfortable with it!

  • @rezahosseini7851
    @rezahosseini7851 4 роки тому +4

    I finally did it! I finally learned reduce!! Thank you!

    • @leighhalliday
      @leighhalliday  4 роки тому

      I'm proud of you Reza!! Thanks for your support :)

  • @araazizi7908
    @araazizi7908 3 роки тому

    This video single handedly taught me A LOT of stuff that used to make JS look like a big question mark in my head lol kudos

  • @Ahmed-cy4jp
    @Ahmed-cy4jp 2 роки тому

    This is my very favorite tutorial. I finally learned how this awesome method works, and in fact I built my own version of reduce and it basically returned the exact same outputs that the built-in reduce method returns!
    Thank you so much

    • @leighhalliday
      @leighhalliday  2 роки тому +1

      Nice!! That’s awesome Ahmed! Glad it helped.

    • @Ahmed-cy4jp
      @Ahmed-cy4jp 2 роки тому

      @@leighhalliday I used all the examples you shared on this video, to make sure it produces the same output, and it actually did :D

  • @diogosoares1309
    @diogosoares1309 3 роки тому

    Loved the flatten one and the counting! Your simplicity explaining is superb, Leigh!

    • @leighhalliday
      @leighhalliday  3 роки тому

      Thank you so much Diogo! I love playing with data like this.

  • @victorlindh3129
    @victorlindh3129 2 роки тому

    Awesome examples! Thank you so much for this!

  • @jpetty5542
    @jpetty5542 2 роки тому

    Great vid! Very good examples and explanations. By far this was the best video on reduce I found.... And I've watched a lot lmao. Thanks again

  • @cliffclof
    @cliffclof 2 роки тому

    @21:14 i think this is what he meant by hand implement a reduce. Nice overview.

  • @rorycawley
    @rorycawley 2 роки тому

    Great vid, thank you Leigh.

  • @soltiscd
    @soltiscd 4 роки тому +1

    This is the way to learn and understand. Love all your content.

  • @slickchick8292
    @slickchick8292 2 роки тому

    I finally understand how to use reduce. I have been so scared of using it. Thank you for this video.

    • @leighhalliday
      @leighhalliday  2 роки тому

      Nice! Glad it helped :) Do not fear reduce!

  • @mehinhuseynova1426
    @mehinhuseynova1426 2 роки тому

    Thank you. Explanation and examples are perfect.

  • @x4r7
    @x4r7 3 роки тому

    best reduce explanation

  • @kissromeorichard
    @kissromeorichard 2 роки тому

    Thank you so much for the explanation and examples!
    Yep, you definitely made me like reduce and not hate it :D

  • @videostothepoint5505
    @videostothepoint5505 3 роки тому

    Superb explanation. Thank you.

  • @ridl27
    @ridl27 4 роки тому +3

    thank you. probably the only one array method that I always trying to avoid lol xD
    but now, hopefully, I will use it as much as I can! :D

  • @vaylx2253
    @vaylx2253 2 роки тому +1

    Hey Leigh, I can't believe it took my so long to discover this channel! This video is an absolute blessing 💛
    One question for you, in the "Convert to ID => Person Lookup (dict)" reduce function, you used the computed property name shorthand by writing: "[person.id] : person" to set the key to "person.id". Kindly, could you tell me how to achieve this without using the shorthand? Like how would you implement this manually?
    Many thanks!

    • @leighhalliday
      @leighhalliday  2 роки тому +1

      Hey Vayl! You can totally do it in multiple lines... something like this?
      acc[person.id] = person;
      return acc;

  • @omedjamal1056
    @omedjamal1056 3 роки тому

    Thanks for this great explanation.

    • @leighhalliday
      @leighhalliday  3 роки тому

      You're very welcome! Glad you enjoyed it Omed!

  • @MariaMartinez-bz7zw
    @MariaMartinez-bz7zw 2 роки тому

    Great video! Thank you so much!

  • @marcellvieira3713
    @marcellvieira3713 4 роки тому

    Spectacular! Greetings from Venezuela

    • @leighhalliday
      @leighhalliday  4 роки тому

      Thank you! Right back at you from Canada :)

  • @fernandoortiz1849
    @fernandoortiz1849 3 роки тому

    thank you for not using a freaking sum of nums example to explain reduce, thumbs up!

  • @juhandvan
    @juhandvan 4 роки тому +1

    thank bro, i really love this tutorial

    • @leighhalliday
      @leighhalliday  4 роки тому

      Thank you Juhand! Glad you enjoyed it!

  • @CoryTheSimmons
    @CoryTheSimmons 4 роки тому +1

    >Reduce may be the most versatile function in JavaScript
    1000% agree. Almost every other function can be replicated with reduce.
    I reach for this pattern almost anytime I need to cleanup an array of objects:
    ```js
    arrOfObjs.reduce((acc, curr) => {
    const freshObj = {}
    freshObj.someKey = curr.someVal
    acc.push(freshObj)
    return acc
    }, [])
    ```

    • @gidmanone
      @gidmanone 4 роки тому

      couldn't you simply map?

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      Thanks Cory! To gidmanone's point, yup I think you could use map for this if you are doing a 1 for 1 mapping of array elements to array elements.

  • @demarco6967
    @demarco6967 4 роки тому +1

    This is very helpfull. thanks so much leigh 🔥😄

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      Thanks Primordial!! Glad you enjoyed it :)

  • @emersonaraujo406
    @emersonaraujo406 4 роки тому

    Congratulations for always bringing us top content. Thanks for your work.

  • @abelmurua6980
    @abelmurua6980 3 роки тому

    Amazing video! Awesome examples. Im having issues with moderno js where you dont use else or curly braces, etc.. so I watse time on that. This helps.

  • @erikshumkov4848
    @erikshumkov4848 3 роки тому

    Thanks for sharing, great examples! Now it's time to implement some of these.

    • @leighhalliday
      @leighhalliday  3 роки тому

      Oh yea! Reduce is so useful! I hardly ever use recursion, but I use reduce almost daily.

  • @thetech3624
    @thetech3624 Рік тому

    I had a question at 15:29, the count occurrences example. Is the accumulator always a number but returns what you want (in that case an object) at the end? Sort of like a double functioning tool? Also, what did you mean by '2 or 5' at 15:16? And overall, I'm not really sure how `acc[order.status]` returns a number. All help is appreciated, thank you.

  • @Booboosik
    @Booboosik 3 роки тому +1

    For "max age" @7:50 use Math.max with comparing to -Infinity (negative infinity), that will make any negative number larger than the initial comparison.

    • @leighhalliday
      @leighhalliday  3 роки тому

      Thanks!! Good trick! Or you can use the first value of the array

  • @pegahfallah3770
    @pegahfallah3770 3 роки тому

    this was a great video

  • @joelexperienced
    @joelexperienced 2 роки тому

    hey man! thanks for the practice :)

  • @coolworx
    @coolworx Рік тому

    I love reduce...
    const activities = ['hiking', 'cycling', 'programming', 'photography',
    'fishing', 'hunting', 'camping' ];
    const list = activities.reduce((ul, activity) => {
    const li = document.createElement('LI');
    li.innerText = activity;
    ul.appendChild(li);
    return ul;
    }, document.createElement('UL'));

  • @elkhanhamet2561
    @elkhanhamet2561 4 роки тому

    thank you Leigh!!!

  • @adityadubey5204
    @adityadubey5204 2 роки тому

    Thankyou so much for this really needed this :)

  • @TheBayouCritter
    @TheBayouCritter 2 роки тому

    This is great, thanks so much. I don't quite get why we need a separate function on the flatten example, is reduce just not capable of calling itself?

    • @leighhalliday
      @leighhalliday  2 роки тому

      No, i don’t believe you can call yourself in a reduce… need a separate named function.

  • @SLRModShop
    @SLRModShop 2 роки тому

    Brilliant video, everyone else is showing the bare minimum...

  • @thanapolraktham2291
    @thanapolraktham2291 3 роки тому

    Thanks a lot!

  • @rajeshsahu3073
    @rajeshsahu3073 4 роки тому +1

    Well explained video..Your way of teaching is cool..Could you please make a video on how to implement multilevel (3 to 4 levels deep) menu in react?

    • @leighhalliday
      @leighhalliday  4 роки тому

      Hey Rajesh! I'll keep it in mind, but I tend to shy away from the purely UI frontend stuff because that isn't my strong area :D

  • @OsamaQarem
    @OsamaQarem 4 роки тому

    you explain so well!!

  • @gregfletcher2360
    @gregfletcher2360 4 роки тому

    Hey Leigh! Great video, Here's my solution to the condition reducer. I would like to see a better method since I only just hacked it together in a minute.
    function conditionalReducer(array, cb, initial, condition) {
    let acc = initial;
    for (let i = 0; i < array.length; i += 1) {

    acc = cb(acc, array[i], i, array);
    if (condition === acc) return acc;
    }
    return acc;
    }

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      Thanks Greg!! I added my condition to the repo I shared in the description :D no idea if it's a good one haha!

    • @gregfletcher2360
      @gregfletcher2360 4 роки тому

      @@leighhalliday Sweet. I'll check it out! Thanks for the video again! I love the quality of the content!

  • @danielluna7648
    @danielluna7648 3 роки тому

    Question: for short circuiting the accumulator once you find what you're looking for, couldn't you still use reduce, but when you find what you're looking for, assign that value to accumulator, break, then return the accumulator with that new assigned value?

    • @misterl8129
      @misterl8129 3 роки тому

      no, reduce cannot be broken. Just classic for and foreach

    • @misterl8129
      @misterl8129 2 роки тому

      @@capslock3250 totally right i get the wrong names lol, thx

  • @seenaranjith
    @seenaranjith 2 роки тому

    great videos. I have a question about result = people.reduce((acc, person) => {
    return { ...acc, [person.id]: person };
    }, {});. If we are grouping names instead of id and have more than one same name, how can we rewrite this code?.

  • @AndersonSilvaMMA
    @AndersonSilvaMMA 4 роки тому

    Thanks for your video!

    • @leighhalliday
      @leighhalliday  4 роки тому +1

      You're welcome!! Didn't realize Anderson Silva was a JS developer! Love your fights! Obrigado!

  • @emrahburak8684
    @emrahburak8684 3 роки тому

    Wow. Thanks.

  • @gradientO
    @gradientO 4 роки тому +2

    Can you do a video on some react ui components, like Material UI?

    • @leighhalliday
      @leighhalliday  4 роки тому +2

      Hey g g! Maybe at some point in the future... I'm 90% backend dev, so I prefer to stay away from the super visual stuff... it isn't where I feel confident.

  • @haytham6776
    @haytham6776 4 роки тому

    leigh what theme you're using in vscode .?

  • @s21ekosn8
    @s21ekosn8 2 роки тому

    reduce is the most powerful js array method 😃

  • @akashnag3879
    @akashnag3879 3 роки тому

    this is the answer to me reduce method quest. everyone just shows how to just add elements of an array.. that's it

    • @leighhalliday
      @leighhalliday  3 роки тому

      Haha... I tried to make it practical... reduce has so many uses!

  • @CarlosSz
    @CarlosSz 4 роки тому

    Amazing video! Can you please make a video like this with filter? 👀

    • @leighhalliday
      @leighhalliday  4 роки тому

      To recreate filter using reduce, or just to cover filter in general? Here's a minimal reduce example:
      array.reduce((acc, element) => {
      if (true) return [...acc, element]
      return acc
      }, []);

  • @abessesmahi4888
    @abessesmahi4888 4 роки тому

    God bless you

  • @finnbit1155
    @finnbit1155 4 роки тому

    I just watched your mapbox video and I was wondering if you could have the website open so anyone can add markers and add to thoughs details?

    • @leighhalliday
      @leighhalliday  4 роки тому

      Hey Finn!! I think you'd want to:
      1) Listen to click event on map... to get the coordinates.
      2) Ask the user for the details they want to place in this marker.
      3) Send this data to a backend to store it somewhere... otherwise it's lost
      4) Add this marker to your array/state of markers so that when it re-renders it is shown on the map.

  • @mouradaouinat8721
    @mouradaouinat8721 4 роки тому

    amazing video

  • @justinnguyen8320
    @justinnguyen8320 3 роки тому

    You're a beast

  • @migom6
    @migom6 4 роки тому

    Let's go!

  • @creative-commons-videos
    @creative-commons-videos 4 роки тому +1

    theme name please :)

  • @pankajdaira9723
    @pankajdaira9723 Рік тому

    aaaaaaaawwwwwwwwwwweeeeeeeeoooooooossssssssooooommmmmmeeeee.....

  • @soniablanche5672
    @soniablanche5672 3 роки тому

    for the max age, I don't think negative ages make sense lol

    • @leighhalliday
      @leighhalliday  3 роки тому

      10 years before your parents met, were you -10 years old, or null? :D

  • @joostschuur
    @joostschuur 4 роки тому

    Huh. Math.max() treats null as zero, so that would not have helped make the max age one shorter here. Math.max(null, -1) returns zero. Annoying ;)

    • @leighhalliday
      @leighhalliday  4 роки тому

      Ahh... I guess you could filter out non-numeric values prior to using Math.max

  • @sumanthprabhu11
    @sumanthprabhu11 2 роки тому

    I'm not gonna like this video as it has currently 666 likes and i'm superstitious. Otherwise great video