JavaScript Array Reduce

Поділитися
Вставка
  • Опубліковано 13 жов 2024
  • 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:
    programmingwith...
    / programmingwithmosh
    / moshhamedani

КОМЕНТАРІ • 371

  • @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 6 років тому +456

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

  • @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 2 роки тому

      @@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 2 роки тому +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

  • @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 Рік тому

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

  • @RetroRick1990
    @RetroRick1990 4 роки тому +7

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

  • @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 Рік тому

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

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

  • @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.

  • @harwinderthakur9708
    @harwinderthakur9708 4 роки тому +8

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

  • @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..

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

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

  • @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.

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

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

  • @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

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

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

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

    Finally ... some who explain reduce well .Thanks

  • @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.

  • @aiknowledge-n2s
    @aiknowledge-n2s 2 роки тому

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

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

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

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

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

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

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

  • @mavs4life248
    @mavs4life248 7 місяців тому

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

  • @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.

  • @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.

  • @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

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

    actually, it must be accumulator += currentValue but just + works here maybe because of reduce method is holding the current value in memory for every iteration.

  • @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

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

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

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

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

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

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

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

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

  • @sdwhitesox8039
    @sdwhitesox8039 5 років тому

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

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

    Mosh is everything a newbie needs!!!

  • @glantzdesign6239
    @glantzdesign6239 5 років тому +1

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

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

    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.

  • @ryansatriayudha
    @ryansatriayudha 11 місяців тому

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

  • @millertime6
    @millertime6 5 років тому +1

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

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

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

  • @j.k.ravshanovich
    @j.k.ravshanovich 4 роки тому

    Explained better than MDN. Good job!

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

    Best code videos I ever watched. Thank you brother!

  • @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.

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

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

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

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

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

    Thank you for making js clear and simple to understand

  • @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

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

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

  • @md.bulbulislam2326
    @md.bulbulislam2326 3 роки тому

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

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

    Excellent and simplified. Is this method not called object deconstruction?

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

    This explanation is way better than the documentation.

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

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

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

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

  • @mariodmp
    @mariodmp 6 років тому +2

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

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

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

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

    one of the best explaination I ever watched

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

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

  • @ycombinator765
    @ycombinator765 3 роки тому +2

    Amazingly clear!! Functional Programming is the best ❤️

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

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

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

    This is a better description than coding academy.

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

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

  • @jame_sta
    @jame_sta 6 років тому +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!!!!!

  • @EssensOrAccidens
    @EssensOrAccidens 5 років тому +2

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

  • @АннаАгабекян-ю8т
    @АннаАгабекян-ю8т 4 роки тому

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

  • @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.

  • @OMARCODER-zz4si
    @OMARCODER-zz4si 4 роки тому +1

    as usual, you make coding easy ...your explanation is great

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

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

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

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

  • @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.

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

    Excellent explanation of reduce function.

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

    Thank you. Better than any video out there.

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

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

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

    Thank you mosh you explained it perfectly

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

    Your explanation was amazing! By the way, which VS Code theme is that?

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

    Such A smooth Explainer

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

    The best teacher so far

  • @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. 🤩🤩🤩

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

    You are a king in easy explaining

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

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

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

    You are the best instructor mosh

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

    nice explanation please do explain how the fuction and codes work it will be very helpful thanks

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

    Awesome explanation! Thank you for this!

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

    Hi Mosh, great video! Could you make a similar one explaining array sort?

  • @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?

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

    thanks buddy for making concept clear

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

    Nice explanation of the Reduce Method of arrays. Thanks, Mosh
    {2021-08-14}

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

    MDN should hire you to do their video explanations. This was great!

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

    I have tried to do in the old fashion way using for(let i = 0;i

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

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

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

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

  • @NITESHSINGHNRS
    @NITESHSINGHNRS 5 років тому +1

    Best... Thank you sir...love from india.♥️

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

    Very well explained. Thank you.

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

    You explain very well. Thanks!

  • @pranjalruhela1103
    @pranjalruhela1103 Місяць тому

    Accumulator, current value (through iterations) and initial value

  • @code_fu552
    @code_fu552 5 років тому +1

    Amazing!!! Thank u for breaking it down and explaining the components . Very helpful.

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

      ua-cam.com/video/a_Bfu1XWGmI/v-deo.html

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

    very easy to understand by good explanation

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

    you have made it simple man ! kudos

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

    Thank you for the good explanation and help.

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

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

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

    interesting ... i like the way u teaching explaining...thank u so much....and keep it up....

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

    He the best teacher on the internet

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

    simple and great explanation Mosh.

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

    mosh didn't explain this part but you can change the starting value with a last argument after the callback.. he kind of explained it but didn't fully lol... in the example below the starting value and thus the starting accumulator value would be 50.
    const final=numbers.reduce((accumulator, current val)=>{
    return accumulator+current val
    }, 50);

  • @ritsk4338
    @ritsk4338 5 років тому

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

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

    thanks, for your clear explanation