JavaScript Array Reduce
HTML-код
- Опубликовано: 2 окт 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
Finally... Somebody who explains reduce well. MDN documentation didn't help me.
Just came from MDN 'cause I didnt understant a sheep!
Exactly.
You are very right! MDN didnt help at all...
Ikr he is awesome
So much clarity in 7 minutes video !
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.
I love he teaches coding very clearly in a short amount of time. He's the best coding teacher.
ruclips.net/video/a_Bfu1XWGmI/видео.html
Awesome , just understand what is the role of the "Accumulator" and the "Current Value" makes everything very clear. Thank man!
I've explored every single line of MDN reference, but I didn't learn as much I could get from you. Appreciate that mate
Boss more blessing to you, love from Nigeria. You just explained all MDN couldn't in minutes
This video is posted a long time ago, but for the first time i understand reduce. Thanks Mosh!
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"
understand the elegant way and use the readable way if you want, after some months you will like to use the elegant way
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.
Having it explained in video form is so much easier for me than reading it. Thank you!
ruclips.net/video/a_Bfu1XWGmI/видео.html
Thank you Mosh. I wish everyone can explain things as clearly as you.
Thank you for explaining this right! No one seems to explain the first callback value as the accumulator but yet as the previous value.
Thank you dude, this is incredible. I wasn't understanding this before watching your video, thank you.
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.
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.
@@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
@@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
Explained better than MDN. Good job!
This explanation is way better than the documentation.
Mosh, thank you so much! You broke this down into a very easy to understand way.
You're the BEST Instructor I've ever come across +ProgrammingwithMosh
*sheds a tear* LOL :D -- I can't wait for more JS videos!!!!!
Excellent explanation of reduce function.
The best teacher so far
You are a king in easy explaining
He the best teacher on the internet
very easy to understand by good explanation
Thank you for the good explanation and help.
thanks buddy for making concept clear
I spent 20 minutes on mdn understanding all this. I got more confused there.Thanks man
Very important topic explained easily.
best coding teacher :)
This is nice but it'd be great if there was something on how to use reduce when making new objects/arrays
Very helpfull and clear as well in the explaination MDN was making me crazy. Thanks mosh! :)
It was really clear explanation. Thanks a lot Mosh!
Thank you sir for explaining this so clearly.
Thank you was having trouble understanding it
Nice class, thanks for this content!
Nice explanation, thanks 😊
Wow, so clear. Thanks, Mosh!!
No entiendo bien el ingles, y entendí el uso del Reduce super bien :0, u eso creo, jaja Muchas gracias!
jajaj mosh es muy bien!
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.
Thank you very much for this video! It helps a lot and the examples make it clear :)
Very well explained, thank you!
thanks for great explanation!
Amazing explanation dude !!! I actually do prefer to use map, filter, sort and reduce function as i have read on a paper that imperative languages as Javascript, the high order functions works really faster than looping or with recursion. What do you think about that, it is like that ? Greetings and thanks again !
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?
excellent tutorial but i think the for loop still looks cleaner
finally, I can totally understand the reduce method thanks to you, appreciate your channel
well explained, thanks.
Thank you so much. This was such a huge help!
That's exactly what I needed. Thanks.
Really helpful man....
Easy and simple, thank you!
very clear thank you so much
awasom very good explanation
great explanation Mosh..
[...Array(1000)].reduce((acc, elem) => {
return acc + 'Thanks Mosh ';
}, ['Great Explanation ']);
can you give some other examples of reduce use cases.
now it 2020 but it still good thank you mann for this video !
Amazingly clear!! Functional Programming is the best ❤️
ruclips.net/video/a_Bfu1XWGmI/видео.html
You Are Great..........................
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.
ruclips.net/video/a_Bfu1XWGmI/видео.html
Thank you so much brother,your explanation was very simple and clear
6:39 I felt that
Thank you, Mosh. I found this very helpful for grasping the concept of reduce().
So useful many thanks
Now I finally understand what is really happening with the reduce method.
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.
How did the function taked "numbers" const?
Finally ... some who explain reduce well .Thanks
Hi Mosh, great video! Could you make a similar one explaining array sort?
Omg I wouldn't learn it without this video. Still I think for loop is easier ;) I think reduce is fancy way of coding :D I don't like it but that's the way you see people code in real job :D
Thank you for making js clear and simple to understand
But why the second argument in numbers.reduce() (zero) goes to "accumulator"?
it is the implementation of the function reduce(). A implementation could be like that:
const reduce = function(iterable, reduceFn, accumulator){
for(let i of iterable){
accumulator = reduceFn(accumulator, i)
}
return accumulator
}
? each time we add to the accumulator.
the current value starts at 0
mosh didn't explain this part but you can change the starting value with a last argument after teh callback.. in the exmple below the starting value and starting accumulator value would be 50.
const final=numbers.reduce((accumulator, current val)=>{
return accumulator+current val
}, 50);
we start by adding the first number in the array to 50(or the first current value we are working with) then if that result was 52... then the accumulator is 52, then the current value moves on to the next index number in the array and keeps going until it adds each value in the array together onto the accumulator
basically the current value is the current value in the array we are currently working with. each array method does whatver the callback says to do on EACH value in the array. the current value in moshes example becomes 0 at one point because we add -1 to 1... which is 0
hope this helped?
It’s an optional argument specific to reduce methods that instruct the accumulator as to which element to begin. If no initial value is provided (the number you’re speaking of), the first element of the array is used as the accumulator’s starting value, and the iteration starts from the second element.
this was fucking awesome! I felt guilty for not subscribing :D
This is by far the clearest explanation I've seen on the reduce method so far.. Thanks a lot Mr Mosh..
Accumulator, current value (through iterations) and initial value
Thank you!
Hi , Anyone knowwhy when i use:
const numbers = [1, -1, 2, 3];
let sum = 0;
for (let n in numbers)
sum += n;
console.log("sum: " + sum);
getting - sum: 00123
Cleanest explanation I've seen so far. You just earned yourself a sub.
But why 'minus' operator in reduce function results in -3, when it's obviously -5? (when not specifying index)
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);
Looking at the comments below people have spent ages trying to understand the reduce function. Why? Cos just like everything in Javascript its BS! :6:43 Mosh talks about the old way - using For loops. Well at least for loops are self evidently obvious! If i'm debugging code I don't want to spend an eternity trying to understand symbols that have no apparent meaning or reference!
hay quá, cảm ơn bạn
Wow! Explained in a super easy way. Thank you.
THANK YOU
Nice explanation of the Reduce Method of arrays. Thanks, Mosh
{2021-08-14}
Took me a while to get reduce even after learning all the other iteration methods. Thank you!
This is very clear and simple explanation!! Thank you very much 😊
Lovely explaination and lovely Mosh.
We Love You Mosh, From Iran.
Thank u for the great explanation
let num = numbers.reduce((a,c) => a+=c ,2)
This is just the simplest of reduce function it is far more complicated than this. It is the most complicated array method and using it for sum is not really the objective.
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?
Thank you Mosh. This is one of the best explanations about how to use reduce, that I could find on RUclips.
The way this is presented seems to suggest that the reduce method is only used to sum all the values in an array. You have an accumulator and a current value. However, it's my understanding you can do other calculations with reduce to get at a single value. How would you find the maximum number in an array of numbers, for example, using reduce? What is the function of the accumulator and current value in that case? I find this demo very clear in how it works with summing all the values in an array, but it really doesn't show how reduce works in the real world, and I'm afraid it's a little misleading.
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.
Excellent and simplified. Is this method not called object deconstruction?
thank you for explaning the basic concepts very clear...best explanation
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?
interesting ... i like the way u teaching explaining...thank u so much....and keep it up....
Thank you for breaking this down in detail. Very easy to understand.
Your explanation was amazing! By the way, which VS Code theme is that?
Best... Thank you sir...love from india.♥️
actually, I like old way much better. javascript simply sucks.
Thanks a lot! Very good explaination ;)