JavaScript reduce() method in 5 minutes! ♻️
Вставка
- Опубліковано 5 вер 2024
- // .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);
}
// .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);
}
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!!
just restart
I did this :
let username = ["Mr","Adam","Gassouma"];
let full = username.reduce(fullname);
console.log(full);
function fullname(previous,next){
return previous+" " + next;
}
This video is absolutely brilliant and so so clear. I love your videos. Thank you.
Very good explaination dude..its more help full to me
now i know why your channel name is bro code ................. :)
shame you didn't include the initial value parameter too. Otherwise, very educational.
This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal.
very good explaination. thanksSoMuch~
Dude I've learned a lot from your videos !!
Thank you brother, you helped me a lot, May god bless you !!
//Using Arrow Function:
//Arrow Function is awesome :D
const grades = [70, 65, 75, 89, 94];
const maximum = grades.reduce((previous, next) => Math.min(previous, next));
console.log(maximum);
I rarely comment.
You're awesome!
oh my god you saved my life thank you!!!!!
would be a way better if you added a initialValue parameter but thank you though
Bro you should do a project video.
Do a node js video
GOD BLESS YOU BRO!
good video, but what about objects? u forgot those.
Thanks Brooo👊
is there any difference between defining the callback that way and using arrow notation inside reduce like prices.reduce((accum, el) =>{...})
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
@@ianfrye8988 thank you
thank you so much!
django course please
thank you so much
You re the man thx bro
Thank u bro
thank you dude
Thanks bro!!!!!!
thanks again
Also all other JavaScript higher order array methods can be created with reduce