๐ŸŸจ JavaScript/๊ฐœ๋…

[๋“œ๋ฆผ์ฝ”๋”ฉ] ์œ ์šฉํ•œ ๋ฐฐ์—ด(array) api (3) : reduce, reduceRight, sort, api ๋‹ค์ค‘์œผ๋กœ ์“ฐ๊ธฐ

Zoeeey 2022. 6. 15. 12:57
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)ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฆฌ๋“€์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ๋„ค ๊ฐœ์˜ ์ธ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.

  1. ๋ˆ„์‚ฐ๊ธฐ(acc, accumulator)
  2. ํ˜„์žฌ๊ฐ’(cur, currentValue)
  3. ํ˜„์žฌ ์ธ๋ฑ์Šค(idx, currentIndex)
  4. ์›๋ณธ ๋ฐฐ์—ด(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