JavaScript reduce() method in 5 minutes! ♻
// .reduce() = reduce the elements of an array
// to a single value
// ---------- EXAMPLE 1 ----------
const prices = [5, 30, 10, 25, 15, 20];
const total = prices.reduce(sum);
console.log(`$${total.toFixed(2)}`);
function sum(accumulator, element){
return accumulator + element;
}
// ---------- EXAMPLE 2 ----------
const scores = [75, 50, 90, 80, 65, 95];
const maximum = scores.reduce(getMax);
const minimum = scores.reduce(getMin);
console.log(maximum);
console.log(minimum);
function getMax(accumulator, element){
return Math.max(accumulator, element);
}
function getMin(accumulator, element){
return Math.min(accumulator, element);
}
Пікірлер: 30
// .reduce() = reduce the elements of an array // to a single value // ----------- EXAMPLE 1 ----------- const prices = [5, 30, 10, 25, 15, 20]; const total = prices.reduce(sum); console.log(`$${total.toFixed(2)}`); function sum(accumulator, element){ return accumulator + element; } // ----------- EXAMPLE 2 ----------- const scores = [75, 50, 90, 80, 65, 95]; const maximum = scores.reduce(getMax); const minimum = scores.reduce(getMin); console.log(maximum); console.log(minimum); function getMax(accumulator, element){ return Math.max(accumulator, element); } function getMin(accumulator, element){ return Math.min(accumulator, element); }
@user-mk2md9pu5m
7 ай бұрын
Django course please
Dude, thank you SO much, the way you've explained how the three data transformation array methods work, as well as the how the forEach loop works, is incredibly easy to understand. I've finally got my head around how the forEach loop works, and now JS is (ifnally) starting to click with me. Again, thank you, and all the best!!
@javohir1704
Ай бұрын
just restart
You said that the first iteration would be sum(0, 5), but that is not true. If you don't set the initial value for reduce the first value will be taken as 1 element of the array and the first iteration will be sum(5, 30).
@RafaelSilva-rv6kt
3 ай бұрын
Yeah, if you think about it, the minimum value would be 0 if that was the case, and not 50
@iluvx360
14 күн бұрын
Good catch
Dude I've learned a lot from your videos !!
This video is absolutely brilliant and so so clear. I love your videos. Thank you.
Thank you brother, you helped me a lot, May god bless you !!
thank you so much!
You re the man thx bro
thank you so much
Thanks bro!!!!!!
thank you dude
Thank u bro
Bro you should do a project video.
thanks again
is there any difference between defining the callback that way and using arrow notation inside reduce like prices.reduce((accum, el) =>{...})
@ianfrye8988
7 ай бұрын
For the most part no, just depends on if you’re going to reuse that function… when you get into the “this” keyword it does depending on the context
@pexhay4690
7 ай бұрын
@@ianfrye8988 thank you
Do a node js video
now i know why your channel name is bro code ................. :)
would be a way better if you added a initialValue parameter but thank you though
shame you didn't include the initial value parameter too. Otherwise, very educational.
good video, but what about objects? u forgot those.
django course please
I did this : let username = ["Mr","Adam","Gassouma"]; let full = username.reduce(fullname); console.log(full); function fullname(previous,next){ return previous+" " + next; }
Also all other JavaScript higher order array methods can be created with reduce