JavaScript Array Reduce

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ค. 2018
  • JavaScript Array Reduce
    🔥Get the COMPLETE course (83% OFF - LIMITED TIME ONLY): bit.ly/2KZea52
    Subscribe for more videos:
    / @programmingwithmosh
    Want to learn more from me? Check out my blog and courses:
    programmingwithmosh.com
    / programmingwithmosh
    / moshhamedani

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

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

    Once again Mosh shows why he's one of the best coding teachers. You make things very clear, because you explain what EVERYTHING is, in laymen's terms. A lot of coding teachers forget they're teaching beginners.

  • @albatros280
    @albatros280 5 ปีที่แล้ว +451

    Finally... Somebody who explains reduce well. MDN documentation didn't help me.

    • @ginnerzapata5909
      @ginnerzapata5909 5 ปีที่แล้ว +40

      Just came from MDN 'cause I didnt understant a sheep!

    • @monfernape
      @monfernape 5 ปีที่แล้ว +7

      Exactly.

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

      You are very right! MDN didnt help at all...

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

      Ikr he is awesome

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

      So much clarity in 7 minutes video !

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

    How am I only finding your tutorials now. You explain so well. I'm taking you with me for the rest of my Java script journey.

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

    Been looking high and low for a simple explanation of the reduce method, and only now I have found it. Thank you.

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

    This is by far the clearest explanation I've seen on the reduce method so far.. Thanks a lot Mr Mosh..

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

    The English is just so perfect for a non-native speaker like me. The explanation is so easy to understand too. Thank you so much, you get my subscription!

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

    Awesome , just understand what is the role of the "Accumulator" and the "Current Value" makes everything very clear. Thank man!

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

    I've explored every single line of MDN reference, but I didn't learn as much I could get from you. Appreciate that mate

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

    Boss more blessing to you, love from Nigeria. You just explained all MDN couldn't in minutes

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

    Thank you Mosh. I wish everyone can explain things as clearly as you.

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

    By far the best explanation of reduce I have gotten haven watch tones of videos and tutorials

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

    finally, I can totally understand the reduce method thanks to you, appreciate your channel

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

    Thank you Mosh. This is one of the best explanations about how to use reduce, that I could find on TH-cam.

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

    This video is posted a long time ago, but for the first time i understand reduce. Thanks Mosh!

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

    Great tutorial, very well explained and easy to follow, I can finally say I understand the reduce method. Thanks you! Hope to see more tutorials like this in the future, like this key word, classes, call, bind, apply, etc.

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

    Thank really. I was making a project. I am adding tips of bills as % to a calculation machine and i need to sum all tips and print. At the end i succeeded by looking to your tutorial. Thanks so much.

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

    Hadn't got used to that reduce method and was struggling for a while, only took me a minute of your explanation to spark a lightbulb in my head and I felt relieved. Thank you.

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

      th-cam.com/video/a_Bfu1XWGmI/w-d-xo.html

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

    I spent 20 minutes on mdn understanding all this. I got more confused there.Thanks man

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

    Thank you for explaining this right! No one seems to explain the first callback value as the accumulator but yet as the previous value.

  • @Gamer-gw6nj
    @Gamer-gw6nj ปีที่แล้ว

    The first tutorial that made me understand reduce method. Thanks.

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

    This is super detailed, yet simple to understand, just great!

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

    Took me a while to get reduce even after learning all the other iteration methods. Thank you!

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

    You're the BEST Instructor I've ever come across +ProgrammingwithMosh
    *sheds a tear* LOL :D -- I can't wait for more JS videos!!!!!

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

    After watching this higher education, I was finally able to deeply understand it. Thank you!

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

    Cleanest explanation I've seen so far. You just earned yourself a sub.

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

    Best method of teaching reduce method from complicated to its simplest possible form all under 10mins. You are amazing good sir. Thank you

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

    Excellent explanation. I was confused how it worked until I saw this. Keep doing what you're doing man!

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

    Thank you, Mosh. I found this very helpful for grasping the concept of reduce().

  • @mavs4life248
    @mavs4life248 4 หลายเดือนก่อน

    Thank you for breaking this down in detail. Very easy to understand.

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

    Thank you so much for this tutorial! Love the no-nonsense approach that any layman can pick up on :)

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

    i just bought a 100 algorithms course from udemy.com. I prefer the way you teach. You explain everything thoroughly, the course instructor seemed to rush through the videos.

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

    Can anyone explain why for loop or foreach is "old way" and reduce is more "elegant way"?
    I find the"old way" more readable than the "elegant way"

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

      understand the elegant way and use the readable way if you want, after some months you will like to use the elegant way

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

      I also think the "old way" is better. The "elegant way" is limited to iteration of type y_n = f(y_{n-1}, x_n) where f is the callback function. There is this new movement in programming where people think old fashion for-loops are ugly code.

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

    Thank you dude, this is incredible. I wasn't understanding this before watching your video, thank you.

  • @zdargahi
    @zdargahi 5 ปีที่แล้ว +68

    It would be great if you explain how the accumulation can be an array or object, and how when instead of chaining map and filter we can use reduce. I guess it's in the complete course.

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

      If you were using an array, you could return an array in the reduce, and spread the accumulator. Then add any new value after. Example:
      nums = [1,2,3,4,5]
      const doubledNums = nums.reduce( (acc, num) => [...acc, num*2], [ ])
      console.log(doubledNums) // [2,4,6,8,10]
      Obviosuly a .map could do the same thing but there are more complex examples where it is useful.

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

      @@liamwelsh5565 Hi, I'm very new to coding, may I know why did you add the "..." before acc? I'm not really sure what it does here

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

      @@Xetron1978 … is the spread operator. If you have an array, let’s call x and want to make a new array, let’s call y, that has all the values of x, you can spread x into y.
      Example:
      Const x = [1,2,3]
      Const y = […x]
      Console.log(y)
      // [1,2,3]
      You can also spread objects into objects

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

    I'm from Brazil, but you explained so well. Thanks a lot!

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

    Literally just spent an hour trying to figure this out from reading on MDN. I understood beforehand how reducing works. I'm familiar with the concept from a few other languages, which implement it more elegantly through lambda expressions. Javascript syntax can be odd at first, and it all came down to simple syntax errors I was making.

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

    Best code videos I ever watched. Thank you brother!

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

    I love he teaches coding very clearly in a short amount of time. He's the best coding teacher.

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

      th-cam.com/video/a_Bfu1XWGmI/w-d-xo.html

  • @user-yz6jf5vv6h
    @user-yz6jf5vv6h 4 ปีที่แล้ว

    Hooray, finally someone explained normally .. thank you very much!

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

    Mosh, thank you so much! You broke this down into a very easy to understand way.

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

    Finally ... some who explain reduce well .Thanks

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

    This is nice but it'd be great if there was something on how to use reduce when making new objects/arrays

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

    Mosh is everything a newbie needs!!!

  • @md.bulbulislam2326
    @md.bulbulislam2326 2 ปีที่แล้ว

    Thank you so much for the beautiful explanation of reduce the method.

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

    Wow, so I was trying to understand this by reading it on MDN and it was just not sinking in. This helped SO much! Thank you good sir.

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

    Man, amazing explanation! So much better than some of the videos and websites I have seen on this topic!

  • @ryansatriayudha
    @ryansatriayudha 8 หลายเดือนก่อน

    This is very clear and simple explanation!! Thank you very much 😊

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

    I was totally in the dark with this Reduce thing...thanks for explaining🙏 great help 👏👏👏

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

    "Just check MDN" does not help. These videos are the only things out there that actually explain anything. Thanks Mosh.

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

    Having it explained in video form is so much easier for me than reading it. Thank you!

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

      th-cam.com/video/a_Bfu1XWGmI/w-d-xo.html

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

    This was VERY helpful and clear. Thanks, Mosh

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

    Thank you for making js clear and simple to understand

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

    thank you for explaning the basic concepts very clear...best explanation

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

    Cheers, nice way of explaining things, I always pass ur videos when it pop ups now I watch and it's wonderful.

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

    Wow! Explained in a super easy way. Thank you.

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

    Lovely explaination and lovely Mosh.
    We Love You Mosh, From Iran.

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

    Thank you. Better than any video out there.

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

    one of the best explaination I ever watched

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

    Wow this guy is simply the best and you cannot dispute that

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

    Please having one problem in the for loop why he doesn't include curly braces bro please does mosh installed any extension to do so?? Please someone reply

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

    wonderful explanation!!! tnx for been there Mosh!!!

  • @ginger-bread_man
    @ginger-bread_man 4 ปีที่แล้ว

    What is at the advantage of "reduce"? Is it just syntactic sugar or is there a performance gain?

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

    Is there other benefits of using .reduce() method other than the readability and cleaner code?

  • @j.k.ravshanovich
    @j.k.ravshanovich 4 ปีที่แล้ว

    Explained better than MDN. Good job!

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

    Great vid , helped me to finally jump that mental hurdle.

  • @Chief_Sir_E.C.O._Nwuju
    @Chief_Sir_E.C.O._Nwuju ปีที่แล้ว +1

    Thank you so much brother,your explanation was very simple and clear

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

    How did the function taked "numbers" const?

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

    This made my brain hurt a lot less than the lesson I was reading. Thank you!

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

    Thank you very much for this explanation . It was really useful. Keep up the good work.

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

    Awesome explanation! Thank you for this!

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

    Thank you sir for explaining this so clearly.

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

    great explanation Mosh..
    [...Array(1000)].reduce((acc, elem) => {
    return acc + 'Thanks Mosh ';
    }, ['Great Explanation ']);

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

    Thank you mosh you explained it perfectly

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

    Finally!!! Well explained in details. You’re a good man

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

    Excellent explanation of reduce function.

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

    You are a king in easy explaining

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

    Great video on reduce(). Thankyou you have clear all my queries and doubts

  • @ill-fatedstranger447
    @ill-fatedstranger447 6 ปีที่แล้ว

    Quite Good tutorial with some good examples how actually reduce works. Keep it up

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

    But why 'minus' operator in reduce function results in -3, when it's obviously -5? (when not specifying index)

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

    thanks buddy for making concept clear

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

    You explain very well. Thanks!

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

    It was really clear explanation. Thanks a lot Mosh!

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

    Easy and simple, thank you!

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

    Does anyone know if we can change the function to where it SUBTRACTS and accumulator and aggregator?
    Also, why the need to write out an entire function if a method is supposed to be a shortcut for this?

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

    Excellent breakdown as usual, cool new js feature

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

    Thank you Mosh!!! This ruled!!!

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

    You’re the big boss. 😎😎😎 I went through other websites and videos but your explanation was the best.
    Thank you very much! I had the answer all time at home, since I was subscribed to your channel before. 🤩🤩🤩

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

    What if you had multiple objects with multiple properties nested inside an array and you wanted to use .reduce on the same property for each object? Just need to nest everything properly?

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

    what theme are you using?

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

    Very well explained. Thank you.

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

    Thank u for the great explanation
    let num = numbers.reduce((a,c) => a+=c ,2)

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

    why in the for loop he doesnt put curly braces please someone help if some extension he add for it please

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

    You are the best instructor mosh

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

    thanks, for your clear explanation

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

    you have made it simple man ! kudos

  • @golden-dragon1442
    @golden-dragon1442 3 ปีที่แล้ว +1

    hi Mosh thank you for the video. one thing i didnt understand was the second argument (0), i understand that it represents the number 1 (being the first on teh array) but what if thaht number is 3 or 10 what will happen when the code logs?

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

    Thank you so much, wonderful explain

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

    Very helpfull and clear as well in the explaination MDN was making me crazy. Thanks mosh! :)

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

    Wow very clearly. Many thanks

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

    Such A smooth Explainer