JavaScript reduce() method in 5 minutes! ♻️
HTML-код
- Опубликовано: 10 ноя 2023
- // .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
This video is absolutely brilliant and so so clear. I love your videos. Thank you.
Dude I've learned a lot from your videos !!
Thank you brother, you helped me a lot, May god bless you !!
very good explaination. thanksSoMuch~
Bro you should do a project video.
thank you so much!
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
GOD BLESS YOU BRO!
You re the man thx bro
Do a node js video
Thanks bro!!!!!!
thank you so much
thank you dude
Thank u bro
would be a way better if you added a initialValue parameter but thank you though
django course please
shame you didn't include the initial value parameter too. Otherwise, very educational.
thanks again
good video, but what about objects? u forgot those.
now i know why your channel name is bro code ................. :)
I did this :
let username = ["Mr","Adam","Gassouma"];
let full = username.reduce(fullname);
console.log(full);
function fullname(previous,next){
return previous+" " + next;
}
//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);
Also all other JavaScript higher order array methods can be created with reduce