JavaScript reduce() method in 5 minutes! ♻

  Рет қаралды 16,253

Bro Code

Bro Code

6 ай бұрын

// .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);
}

Пікірлер: 26
@BroCodez
@BroCodez 6 ай бұрын
// .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
@user-mk2md9pu5m 6 ай бұрын
Django course please
@Praeda19
@Praeda19 2 ай бұрын
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!!
@geldelian
@geldelian 4 ай бұрын
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
@RafaelSilva-rv6kt 2 ай бұрын
Yeah, if you think about it, the minimum value would be 0 if that was the case, and not 50
@Mochinori99
@Mochinori99 8 күн бұрын
This video is absolutely brilliant and so so clear. I love your videos. Thank you.
@RayhanAsif22
@RayhanAsif22 2 ай бұрын
Dude I've learned a lot from your videos !!
@zrotrasukha8733
@zrotrasukha8733 4 ай бұрын
Thank you brother, you helped me a lot, May god bless you !!
@pini5076
@pini5076 3 ай бұрын
thank you so much!
@pexhay4690
@pexhay4690 6 ай бұрын
is there any difference between defining the callback that way and using arrow notation inside reduce like prices.reduce((accum, el) =>{...})
@ianfrye8988
@ianfrye8988 6 ай бұрын
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
@pexhay4690 6 ай бұрын
@@ianfrye8988 thank you
@magomihaly1741
@magomihaly1741 6 ай бұрын
You re the man thx bro
@islamabdelhakiim7258
@islamabdelhakiim7258 20 күн бұрын
thank you so much
@rukecodes
@rukecodes 6 ай бұрын
Thanks bro!!!!!!
@kathikr9360
@kathikr9360 3 ай бұрын
thank you dude
@manitamao6564
@manitamao6564 2 ай бұрын
Thank u bro
@nomanbangtan4120
@nomanbangtan4120 6 ай бұрын
Bro you should do a project video.
@javohir1704
@javohir1704 Ай бұрын
would be a way better if you added a initialValue parameter but thank you though
@kathikr9360
@kathikr9360 2 ай бұрын
thanks again
@greengraphics5060
@greengraphics5060 4 ай бұрын
Do a node js video
@cyberblitz
@cyberblitz Ай бұрын
shame you didn't include the initial value parameter too. Otherwise, very educational.
@user-mk2md9pu5m
@user-mk2md9pu5m 6 ай бұрын
django course please
@devl0ver666
@devl0ver666 11 күн бұрын
now i know why your channel name is bro code ................. :)
@ianfrye8988
@ianfrye8988 6 ай бұрын
Also all other JavaScript higher order array methods can be created with reduce
JavaScript FUNCTION EXPRESSIONS in 7 minutes! 🐣
7:32
Bro Code
Рет қаралды 10 М.
JavaScript forEach() method in 8 minutes! ➿
8:02
Bro Code
Рет қаралды 21 М.
ONE MORE SUBSCRIBER FOR 6 MILLION!
00:38
Horror Skunx
Рет қаралды 14 МЛН
Reduce это просто. JavaScript
17:11
WebDev с нуля. Канал Алекса Лущенко
Рет қаралды 60 М.
JavaScript Array Reduce
7:42
Programming with Mosh
Рет қаралды 332 М.
How to FETCH data from an API using JavaScript ↩️
14:17
Bro Code
Рет қаралды 81 М.
JavaScript DESTRUCTURING in 8 minutes! 💥
8:41
Bro Code
Рет қаралды 11 М.
Learn the JavaScript Reduce function in 18 minutes (for beginners)
18:23
Learn JavaScript ARROW FUNCTIONS in 8 minutes! 🎯
8:02
Bro Code
Рет қаралды 20 М.
8 Must Know JavaScript Array Methods
10:05
Web Dev Simplified
Рет қаралды 1 МЛН
JavaScript map() method in 7 minutes! 🗺
7:01
Bro Code
Рет қаралды 22 М.
ONE MORE SUBSCRIBER FOR 6 MILLION!
00:38
Horror Skunx
Рет қаралды 14 МЛН