๋ฐ˜์‘ํ˜•

Array.reduce() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ ์š”์†Œ์— ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ˆ„์ ๋œ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š” filter(), map() ๋“ฑ์˜ ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋‘ reduce()๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ํ™œ์šฉ ๋ฐฉ๋ฒ•์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋‹ค. reduce() ๋ฉ”์„œ๋“œ์˜ ๊ตฌ๋ฌธ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

reduce(callbackFn, initialValue?)

 

  1. callbackFn ์ฝœ๋ฐฑ ํŒŒ๋ผ๋ฏธํ„ฐ
    • accumulator : ์ด์ „ callbackFn ๋ฐ˜ํ™˜๊ฐ’
      • ์ดˆ๊ธฐ๊ฐ’ ์ง€์ • ์‹œ ์ฝœ๋ฐฑ์„ ์ฒ˜์Œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๊ฐ’ : initialValue
      • ์ดˆ๊ธฐ๊ฐ’ ๋ฏธ์ง€์ •์‹œ ์ฝœ๋ฐฑ์„ ์ฒ˜์Œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๊ฐ’ : array[0]
    • currentValue : ํ˜„์žฌ ์ˆœํšŒํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’
      • ์ดˆ๊ธฐ๊ฐ’ ์ง€์ • ์‹œ ์ฝœ๋ฐฑ์„ ์ฒ˜์Œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๊ฐ’ : array[0]
      • ์ดˆ๊ธฐ๊ฐ’ ๋ฏธ์ง€์ •์‹œ ์ฝœ๋ฐฑ์„ ์ฒ˜์Œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๊ฐ’ : array[1]
    • currentIndex : ํ˜„์žฌ ์ˆœํšŒํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค
    • array : reduce๋ฅผ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด
  2. initialValue (์ดˆ๊ธฐ๊ฐ’) : callbackFn ์ฝœ๋ฐฑ์„ ์ฒ˜์Œ ํ˜ธ์ถœํ•  ๋•Œ accumulator์— ์‚ฌ์šฉํ•  ์ดˆ๊ธฐ๊ฐ’. ๋ฏธ์ง€์ •์‹œ ๋ฐฐ์—ด(array) ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ. ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •์‹œ ๋ฐฐ์—ด ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์ˆœํšŒ์— ํฌํ•จ ์•ˆ ํ•จ.

 

 

์ˆซ์ž ๋”ํ•˜๊ธฐ | Summing Numbers


reduce() ๋ฉ”์„œ๋“œ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ์˜ˆ์‹œ. ์ˆซ์ž๋กœ ์ด๋ค„์ง„ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ชจ๋“  ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const odds = [1, 3, 5, 7, 9, 11];
const sumOfOdds = odds.reduce((acc, cur) => acc + cur);
console.log(sumOfOdds); // 36

 

 

๋ฐฐ์—ด ํŽผ์น˜๊ธฐ | Flattening Arrays


Array.flat()์€ ์ค‘์ฒฉ๋œ ๋ฐฐ์—ด์„ ํŽผ์ณ์„œ ๋‹จ์ผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ๋ฉ”์„œ๋“œ๋‹ค. reduce()๋ฅผ ์ด์šฉํ•˜๋ฉด flat() ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.reduce((acc, cur) => acc.concat(cur), []);
console.log(flattenedArray); // [1, 2, 3, 4, 5, 6]

 

์œ„ ์˜ˆ์‹œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 1 depth๋งŒ ํŽผ์น  ์ˆ˜ ์žˆ๋‹ค. 2 depth ์ด์ƒ ์ค‘์ฒฉ๋œ ๋ฐฐ์—ด์ด๋ผ๋ฉด ์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

const nestedArray = [1, 2, [3, 4, [5, 6]]];

const deepFlat = (arr) => {
  return arr.reduce((acc, cur) => {
    return acc.concat(Array.isArray(cur) ? deepFlat(cur) : cur);
  }, []);
};

const flattenedArray = deepFlat(nestedArray);
console.log(flattenedArray); // [1, 2, 3, 4, 5, 6]

 

 

๊ฐ์ฒด ๊ทธ๋ฃนํ•‘ | Grouping Objects


๊ฐ์ฒด๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์—์„œ, ํŠน์ • ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘(Grouping) ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์„ ์ž์ฃผ ์ ‘ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์ฒ˜๋Ÿผ age ์—ฐ๋ น๋ณ„๋กœ ๊ทธ๋ฃน์„ ๋‚˜๋ˆ„๋Š” ์ƒํ™ฉ์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

// ์›๋ณธ ๋ฐฐ์—ด
[
  { name: 'A', age: 20 },
  { name: 'B', age: 30 },
  { name: 'C', age: 20 },
  { name: 'D', age: 30 },
];

// ์—ฐ๋ น๋ณ„๋กœ ๊ทธ๋ฃนํ•‘
{
  20: [
    { name: 'A', age: 20 },
    { name: 'C', age: 20 },
  ],
  30: [
    { name: 'B', age: 30 },
    { name: 'D', age: 30 },
  ],
};

 

์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ์ฒด๋กœ ์ง€์ •ํ•œ reduce()๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ทธ๋ฃนํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ age ํ‚ค๋งˆ๋‹ค ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ , ํ˜„์žฌ ์ˆœํšŒํ•˜๋Š” ๊ฐ์ฒด์˜ age ์†์„ฑ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์—ด์— push ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

const users = [
  { name: 'A', age: 20 },
  { name: 'B', age: 30 },
  { name: 'C', age: 20 },
  { name: 'D', age: 30 },
];

const groupByAge = users.reduce((acc, cur) => {
  const { age } = cur;
  if (!acc[age]) acc[age] = [];
  acc[age].push(cur);
  return acc;
}, {});

console.log(groupByAge);
/*
{
  20: [{ name: 'A', age: 20 }, { name: 'C', age: 20 }],
  30: [{ name: 'B', age: 30 }, { name: 'D', age: 30 }]
}
*/

 

์ฐธ๊ณ ๋กœ ๊ฐ์ฒด ๊ทธ๋ฃนํ•‘์€ ES15(ECMAScript 2024)๋ถ€ํ„ฐ ์ œ๊ณตํ•˜๋Š” groupBy() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” Chrome 117 ๋ฒ„์ „, Node.js 21 ๋ฒ„์ „ ์ดํ›„๋ถ€ํ„ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค(Baseline 2024).

const groupByAge = Object.groupBy(users, ({ age }) => age);
console.log(groupbyAge); // {20: Array(2), 30: Array(2)}

 

Browser compatibility via MDN

 

์กฐํšŒ์šฉ ๋งต | Lookup Maps


๋ฐฐ์—ด ๊ฐ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์ด๊ณ , ๊ฐ ๊ฐ์ฒด๋Š” ๊ณ ์œ ํ•œ id ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ๋•Œ, id๋กœ ๋น ๋ฅด๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” Lookup ๋งต์„ ์ƒ์„ฑ ํ•ด๋‘๋ฉด, ํŠน์ • id๋ฅผ ๊ฐ€์ง„ ์š”์†Œ์— O(1) ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งค๋ฒˆ find() ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋กœ ์ˆœํšŒํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ ๋ฉด์—์„œ ๋” ์œ ๋ฆฌํ•˜๋‹ค.

const products = [
  { id: 1, name: 'iPhone' },
  { id: 2, name: 'iPad' },
  { id: 3, name: 'iMac' },
];

const productMap = products.reduce((acc, cur) => {
  acc[cur.id] = cur;
  return acc;
}, {});

console.log(productMap);
/*
{
  1: { id: 1, name: 'iPhone' },
  2: { id: 2, name: 'iPad' },
  3: { id: 3, name: 'iMac' }
}
*/
console.log(productMap[3]); // { id: 3, name: 'iMac' }

 

 

ํšŸ์ˆ˜ ๊ณ„์‚ฐ | Counting Occurrences


๋ฐฐ์—ด ๋‚ด ํŠน์ • ์š”์†Œ๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•  ๋•Œ๋„ reduce() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋นˆ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜๊ณ , ๊ฐ ์š”์†Œ๋ฅผ ๊ฐ์ฒด ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ์นด์šดํŠธ๋ฅผ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

const products = ['iPhone', 'iMac', 'MacBook', 'iPhone', 'iMac', 'iPhone'];

const productCounts = products.reduce((acc, cur) => {
  acc[cur] = (acc[cur] ?? 0) + 1;
  return acc;
}, {});

console.log(productCounts); // { iPhone: 3, iMac: 2, MacBook: 1 }

 

 

ํ•จ์ˆ˜ ํ•ฉ์„ฑ | Composing Functions


ํ•จ์ˆ˜ ํ•ฉ์„ฑ์€ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ, ๊ฐ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์„ ๋‹ค์Œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์€ f(g(x)) ์™€ ๊ฐ™์ด ๋‚ด๋ถ€ ํ•จ์ˆ˜๋ถ€ํ„ฐ ์™ธ๋ถ€ ํ•จ์ˆ˜ ์ˆœ์œผ๋กœ ์‹คํ–‰๋˜๋Š”๋ฐ reduce() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

const compose = (...funcs) => {
  return (x) => funcs.reduce((acc, fn) => fn(acc), x);
};

const increment = (x) => x + 1;
const square = (x) => x * x;
const decrement = (x) => x - 1;

// (x) => func.reduce((acc, f) => f(acc), x)
const composedFunc = compose(increment, square, decrement);
console.log(composedFunc(5)); // 35

 

์œ„ ์˜ˆ์‹œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.

// imcrement -> square -> decrement
decrement(square(increment(5)));

 

๋งŒ์•ฝ ํ•จ์ˆ˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ์š”์†Œ๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋Š” reduceRight() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

์ค‘๋ณต ์ œ๊ฑฐ | Deduplication


๋ฐฐ์—ด์˜ ์ค‘๋ณต ์ œ๊ฑฐ๋Š” Set() ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ์š”์†Œ์— ๋Œ€ํ•ด์„  ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋“ฑ ์ถ”๊ฐ€์ ์ธ ๋กœ์ง์„ ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋Ÿฐ ์ž‘์—… ์—ญ์‹œ reduce() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const numbers = [1, 1, 2, 3, 2, 2, 6, 8, 8, 11, 12];
const allowList = new Set([8]); // 8์€ ์ค‘๋ณต ํ—ˆ์šฉ

const uniqueNumbers = numbers.reduce((res, num) => {
  if (!res.includes(num) || allowList.has(num)) res.push(num);
  return res;
}, []);

console.log(uniqueNumbers); // [ 1, 2, 3, 6, 8, 8, 11, 12 ]

 

 

ํ‰๊ท  ๊ณ„์‚ฐ | Calculating Average


reduce() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ˆซ์ž๋กœ ์ด๋ค„์ง„ ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’์„ ๊ณ„์‚ฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•œ ํ›„, ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋• ์ดํ•ฉ์„ ์š”์†Œ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

const rates = [3, 2, 1, 5, 4, 3];

const average = rates.reduce((total, rate, idx, { length }) => {
  const sum = total + rate;
  if (idx === length - 1) return sum / length;
  return sum;
}, 0);

console.log(average); // 3

 

 

ํ”„๋กœ๋ฏธ์Šค ์ฒด์ด๋‹ | Promise Chaining


์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์€ ์•„๋‹ˆ์ง€๋งŒ reduce() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ”„๋กœ๋ฏธ์Šค ์ฒด์ธ์„ ๊ตฌ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋”œ๋ ˆ์ด ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค prevPromise.then(...) ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ํ”„๋กœ๋ฏธ์Šค๋Š” ๋‹ค์Œ ์ˆœํšŒ์˜ prevPromise๊ฐ€ ๋ผ์„œ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.

const wait = (delay) => {
  return new Promise((resolve, _reject) => {
    console.log('[wait]', delay);
    setTimeout(() => resolve(delay), delay);
  });
};

const delayList = [1000, 2000, 3000, 4000];

delayList.reduce((prevPromise, curDelay) => {
  console.log('[reduce]', prevPromise, curDelay);
  // ์ˆœํšŒ๋งˆ๋‹ค prevPromise.then(...) ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋‹ค์Œ ์ˆœํšŒ์˜ prevPromise๊ฐ€ ๋œ๋‹ค
  return prevPromise.then((res) => {
    console.log('[then]', res);
    return wait(curDelay);
  });
}, Promise.resolve('start'));

/*
์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ํ”„๋กœ๋ฏธ์Šค ์ฒด์ธ๊ณผ ๋™์ผํ•˜๋‹ค: 
Promise.resolve('start')
  .then(() => wait(1000))
  .then(() => wait(2000))
  .then(() => wait(3000))
  .then(() => wait(4000))
*/

 

์ฝ˜์†” ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

๋ชจ๋“  ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋Š” ๋™๊ธฐ์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝœ๋ฐฑ ๋‚ด์—์„œ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด๋„ reduce๋Š” ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์ฝœ๋ฐฑ์„ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์ฆ‰, ์ฝœ๋ฐฑ ๋‚ด์—์„œ ์ˆ˜ํ–‰ํ•œ ๋น„๋™๊ธฐ ์ž‘์—…์€ reduce ๋ฉ”์„œ๋“œ์˜ ์ œ์–ด๋ฅผ ๋ฒ—์–ด๋‚˜ ๋ณ„๋„๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

๋น„๋™๊ธฐ๋กœ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜๋Š” ํƒœ์Šคํฌ ํ๋กœ ๋“ค์–ด๊ฐ€๋Š”๋ฐ, ํ์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์€ ์ฝœ์Šคํƒ์ด ๋น„์–ด์žˆ์–ด์•ผ๋งŒ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ reduce ๋ฉ”์„œ๋“œ์˜ ๋ชจ๋“  ์ˆœํšŒ๋ฅผ ์™„๋ฃŒํ•œ ํ›„์— ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์ด ์ฐจ๋ก€๋Œ€๋กœ ์‹คํ–‰๋œ๋‹ค.

 

๐Ÿ’กthen, catch, finally ๊ฐ™์€ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ ์ธ์ž์— ์ „๋‹ฌํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ•ด๋‹น ๊ฐ’์„ ์•”๋ฌต์ ์œผ๋กœ resolve ํ˜น์€ reject ํ•ด์„œ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋Œ€๊ธฐ(pending) ์ƒํƒœ์ด๋ฉด ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ฒ˜๋ฆฌ๋˜๊ธธ ๊ธฐ๋‹ค๋ฆฌ๊ณ , ์ด๋ฏธ ์ฒ˜๋ฆฌ(settled) ์ƒํƒœ๋ผ๋ฉด ํ•ธ๋“ค๋Ÿฌ๋Š” ์ฆ‰๊ฐ ์‹คํ–‰๋œ๋‹ค.

 

 

Immutable vs Mutable


reduce ๋ฉ”์„œ๋“œ๋Š” ํฌ๊ฒŒ ๋ถˆ๋ณ€(Immutable)๊ณผ ๊ฐ€๋ณ€(Mutable) 2๊ฐ€์ง€ ์‚ฌ์šฉ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถˆ๋ณ€์€ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•œ ๋ฒˆ ์ƒ์„ฑํ•œ ํ›„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๊ฐ’์„, ๊ฐ€๋ณ€์€ ์ƒ์„ฑํ•œ ํ›„์—๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„  ์›์‹œํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ€์ง€๋ฉฐ, ์ฐธ์กฐํ˜•์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€๋ณ€์„ฑ์„ ๊ฐ€์ง„๋‹ค.

 

๋ถˆ๋ณ€์ ์ธ ์‚ฌ์šฉ ๋ฐฉ์‹์€ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด ๋ˆ„์  ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ํ•ญ์ƒ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ธฐ์กด ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๋Œ€์‹ , ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

// Immutable
const doubledNumbers = [1, 2, 3].reduce((acc, cur) => {
  return [...acc, cur * 2];
}, []);

 

๊ฐ€๋ณ€์ ์ธ ์‚ฌ์šฉ ๋ฐฉ์‹์€ ๊ธฐ์กด ๋ˆ„์  ๊ฐ’์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ธฐ์กด ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์„ ์ง์ ‘ ์ˆ˜์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ์€ ๋Œ€์‹ , ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.

// Mutable
const doubledNumbers = [1, 2, 3].reduce((acc, cur) => {
  acc.push(cur * 2);
  return acc;
}, []);

 

๋ถˆ๋ณ€์„ฑ๊ณผ ๊ฐ€๋ณ€์„ฑ ๊ฐ๊ฐ ์žฅ๋‹จ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๋ฐฉ์‹์ด ํ•ญ์ƒ ์˜ณ๋‹ค๊ณ  ๋‹จ์ •ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋ฐ์ดํ„ฐ ์–‘์ด ๋งŽ๊ณ  ์„ฑ๋Šฅ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์—” ๊ฐ€๋ณ€์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋ฉด, ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด๋‚˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ์ค‘์š”ํ•˜๋‹ค๋ฉด ๋ถˆ๋ณ€์„ฑ์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ ˆํผ๋Ÿฐ์Šค


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•