JavaScript reduce() method in 5 minutes! ♻️

preview_player
Показать описание
// .reduce() = reduce the elements of an array
// to a single value

// ----------- EXAMPLE 1 -----------
const prices = [5, 30, 10, 25, 15, 20];

function sum(accumulator, element){
return accumulator + element;
}

// ----------- EXAMPLE 2 -----------
const scores = [75, 50, 90, 80, 65, 95];

function getMax(accumulator, element){
}

function getMin(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);



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

BroCodez
Автор

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!!

Praeda
Автор

This video is absolutely brilliant and so so clear. I love your videos. Thank you.

Mochinori
Автор

Dude I've learned a lot from your videos !!

RayhanAsif
Автор

Thank you brother, you helped me a lot, May god bless you !!

zrotrasukha
Автор

is there any difference between defining the callback that way and using arrow notation inside reduce like prices.reduce((accum, el) =>{...})

pexhay
Автор

I did this :
let username = ["Mr", "Adam", "Gassouma"];
let full = username.reduce(fullname);
console.log(full);
function fullname(previous, next){
return previous+" " + next;
}

AdamGassouma
Автор

would be a way better if you added a initialValue parameter but thank you though

javohir
Автор

now i know why your channel name is bro code :)

devlver
Автор

good video, but what about objects? u forgot those.

allhailalona
Автор

shame you didn't include the initial value parameter too. Otherwise, very educational.

cyberblitz
Автор

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

constdev_
Автор

Also all other JavaScript higher order array methods can be created with reduce

ianfrye