class Coder {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const coders = [
new Coder('A', 29, true, 45),
new Coder('B', 28, false, 80),
new Coder('C', 30, true, 90),
new Coder('D', 40, false, 66),
new Coder('E', 18, true, 88),
]
reduce
๋ฐฐ์ด์ ๊ฐ ๋ฐ์ดํฐ์ ์ฃผ์ด์ง ๋ฆฌ๋์(reducer)ํจ์๋ฅผ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋ค.
๋ฆฌ๋์ ์ฝ๋ฐฑํจ์๋ ๋ค ๊ฐ์ ์ธ์๋ฅผ ๊ฐ์ง๋ค.
- ๋์ฐ๊ธฐ(acc, accumulator)
- ํ์ฌ๊ฐ(cur, currentValue)
- ํ์ฌ ์ธ๋ฑ์ค(idx, currentIndex)
- ์๋ณธ ๋ฐฐ์ด(src)
const result = coders.reduce((prev,curr) => {
console.log('----------');
console.log(prev);
console.log(curr);
return curr;
})
//์๋๊ฐ ๋์จ๋ค.
"----------"
// [object Object]
{
"name": "A",
"age": 29,
"enrolled": true,
"score": 45
}
// [object Object]
{
"name": "B",
"age": 28,
"enrolled": false,
"score": 80
}
"----------"
// [object Object]
{
"name": "B",
"age": 28,
"enrolled": false,
"score": 80
}
// [object Object]
{
"name": "C",
"age": 30,
"enrolled": true,
"score": 90
}
"----------"
.
.
.
์ด์ฒ๋ผ ๋ง์ง๋ง์ return๋ ๊ฐ์ด prev์ ํ ๋น๋๋ค.
initialValue๋ฅผ ์ ๊ณตํ์ง ์์ผ๋ฉด, reduce()๋ ์ธ๋ฑ์ค 1๋ถํฐ ์์ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๊ณ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ ๊ฑด๋ ๋ด๋ค. initialValue๋ฅผ ์ ๊ณตํ๋ฉด ์ธ๋ฑ์ค 0์์ ์์ํ๋ค.
const result = coders.reduce((prev,curr) => {
console.log('----------');
console.log(prev);
console.log(curr);
return curr;
}, 0); //0์ผ๋ก initialvalue๋ฅผ ์ ๊ณตํ๋ค.
"----------"
0
// [object Object]
{
"name": "A",
"age": 29,
"enrolled": true,
"score": 45
}
"----------"
// [object Object]
{
"name": "A",
"age": 29,
"enrolled": true,
"score": 45
}
// [object Object]
{
"name": "B",
"age": 28,
"enrolled": false,
"score": 80
}
"----------"
.
.
.
0๋ถํฐ ๋ชจ๋ score๋ค์ ๋ํด๋ณด์.
const result = coders.reduce((prev,curr) => {
console.log('----------');
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);
console.log(result);
"----------"
0
// [object Object]
{
"name": "A",
"age": 29,
"enrolled": true,
"score": 45
}
"----------"
45
// [object Object]
{
"name": "B",
"age": 28,
"enrolled": false,
"score": 80
}
.
.
.
281
// [object Object]
{
"name": "E",
"age": 18,
"enrolled": true,
"score": 88
}
369
์์ ํจ์๋ฅผ ๋ง์ง๋ง ๊ฒฐ๊ณผ๊ฐ๋ง ๋์ค๋๋ก ๊ฐ๋จํ๊ฒ ๋ฐ๊ฟ๋ณด์.
์
const result = coders.reduce((prev,curr) => {
console.log('----------');
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);
console.log(result);
ํ
const result = coders.reduce((prev,curr) => prev + curr.score, 0);
console.log(result);
์์ ํจ์๋ฅผ ํ๊ท ๊ฐ์ ๊ตฌํ๋๋ก ๋ฐ๊ฟ๋ณด์.
const result = coders.reduce((prev,curr) => prev + curr.score, 0);
console.log(result / students.length);
reduceRight
reduce๋ฅผ ๊ฑฐ๊พธ๋ก (์ ์ผ ๋ค์์๋ถํฐ) ์์ํ๋ค.
Sort
๋ง์ด๋์ค ๊ฐ์ ๋ฆฌํดํ๊ฒ ๋๋ฉด ์ฒซ๋ฒ์งธ๊ฐ ๋๋ฒ์งธ๋ณด๋ค ์๋ค๊ณ ๊ฐ์ฃผ๋์ด ์ ๋ ฌํ๋ค.
const result = coders
.map((coder) => coder.score)
.sort((a, b) => a - b)
.join();
console.log(result);
//"45,66,80,88,90"
* ์ด์ฒ๋ผ compareFunction์ ์ฌ์ฉํ์ง ์์ผ๋ฉด ๊ทธ๋ฅ ๋ฌธ์์ด๋ก ๋ฐ์๋ค์ฌ ์ ๋์ฝ๋์์ผ๋ก ์ ๋ ฌํ๋ค.
** ๋ฐ๋๋ก b-a๋ก ์ฐ๋ฉด ์ซ์ ์ญ์ ์ ๋ ฌ์ด ๋๋ค.
api ๋ค์ค์ผ๋ก ์ฐ๊ธฐ
ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๊ณ ๋ ํ๋ค. ์๋์ฒ๋ผ api๋ฅผ ๋ค์ค์ผ๋ก ์ธ ์ ์๋ค.
const result = coders
.map((coder) => coder.score) //score๋ง ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด ๋ง๋ค๊ธฐ
.filter((score) => score >= 50) //50๋ณด๋ค ํฐ score๋ง ์์๋ด๊ธฐ
.join(); //๋ฐฐ์ด์ string์ผ๋ก ๋ณํํ๊ธฐ
console.log(result);
//"80,90,66,88"
์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko
'๐จ JavaScript > ๊ฐ๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋๋ฆผ์ฝ๋ฉ] Object to JSON (stringify) (0) | 2022.06.29 |
---|---|
[๋๋ฆผ์ฝ๋ฉ] JSON์ด๋? (0) | 2022.06.29 |
[๋๋ฆผ์ฝ๋ฉ] ์ ์ฉํ ๋ฐฐ์ด(array) api (2) : filter, map, some, every (0) | 2022.06.09 |
[๋๋ฆผ์ฝ๋ฉ] ์ ์ฉํ ๋ฐฐ์ด(array) api (1) : join, split, reverse, slice, find (0) | 2022.06.07 |
[mdn, ๋๋ฆผ์ฝ๋ฉ ์ธ] ๋ฐฐ์ด(array) (4) : ๊ฒ์ (indexOf,lastIndexOf,includes) (0) | 2022.06.02 |