๐Ÿฅ˜ ์ค„์ด๊ธฐ: ์œก์ฆ™์ด ๊ฐ€๋“ํ•ด์š”, ์ž๊ธฐ์•ผ!

7771 ๋‹จ์–ด
์ €๋Š” 10๋…„ ๋„˜๊ฒŒ(๐Ÿ‘ด๐Ÿป) JS ์ฝ”๋“œ๋ฒ ์ด์Šค์—์„œ ์ž‘์—…ํ•ด ์™”์ง€๋งŒ ์—ฌ์ „ํžˆ ๊ฐ€๋” ์ €๋ฅผ ๋†€๋ผ๊ฒŒ ํ•˜๋Š” ํ•œ ๊ฐ€์ง€๋Š” reduce ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. MDN documentation on reduce ์— ๊ฐˆ ๋•Œ๋งˆ๋‹ค ์ฒ˜์Œ์—๋Š” ๊ทธ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ๋ฐ ์•ฝ๊ฐ„์˜ ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ํŠธ์œ„ํ„ฐ๋‚˜ ๊ฐ€๋ณด๋ฉด ๋‹ค๋“ค "reduce๊ฐ€ ์ œ์ผ ์ค‘์š”ํ•œ ๋ฐฐ์—ด ํ•จ์ˆ˜์•ผ!"์‹ ํ˜ธ ๋ถˆ์•ˆ ๐Ÿ˜ฉ!

๋”ฐ๋ผ์„œ ์ด ์งง์€ ๊ฒŒ์‹œ๋ฌผ์—์„œ๋Š” Reduce์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ  ์‹ค์ œ๋กœ ๊ด€๋ จ์ด ์žˆ๊ณ  ์••๋„์ ์ด์ง€ ์•Š์€ ์˜ˆ๋ฅผ ๋ณด์—ฌ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๊ทธ๊ฒƒ์œผ๋กœ; ๐Ÿฅ˜๋ฅผ ์ค„์ด์ž.

"๊ณต์‹"๋ฌธ์„œ ์„ค๋ช…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.



์ด๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ์„ ์••๋‹ˆ๋‹ค.
  • ๊ฐ ๋ฐฐ์—ด ์š”์†Œ
  • ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜() => {}๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์ด์ „์— ๊ณ„์‚ฐ๋œ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์™€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ (previousValue, currentValue) => {}
  • ์ถ•์†Œ ํ›„ ์ƒˆ ๊ฐ’ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค
  • .
  • ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ ์‹ค์ œ๋กœ ์•Œ์ง€ ๋ชปํ•˜์ง€๋งŒ reduce๋Š” ์ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ •๋ง ๋ฉ์ฒญํ•œ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. IMOaccumulator๊ฐ€ ํ›จ์”ฌ ๋” ์ •ํ™•ํ•˜๊ณ  ์ •ํ™•ํ•œ ์šฉ์–ด์ž…๋‹ˆ๋‹ค.

  • ํ•œ ๊ฐ€์ง€ ์œ ์˜ํ•  ์ ์€ reduce๊ฐ€ ์ฝœ๋ฐฑ์„ ๋ฐ›์ง€๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ initial value๋„ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ "๋ˆ„์‚ฐ๊ธฐ๋ฅผ ์ž‘๋™์‹œํ‚ค๋Š” ๊ฒƒ"์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๋ฅผ ์ถ”๊ฐ€/์กฐ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ 0 ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

    // This is incredibly similar to the MDN example
    const arrayToReduce = [1, 2, 3, 4, 5];
    arrayToReduce.reduce((prevValue, currValue) => return prevValue + currValue), 0)
    


    ํ•˜์ง€๋งŒ MDN์˜ ์˜ˆ๋ฅผ ๋“ค์–ด ์กฐ๊ธˆ ๋ถ„ํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ž‘๋™ ๋ฐฉ์‹์„ ์ข€ ๋” ์ดํ•ดํ•˜๋ ค๋ฉด console.logs๋กœ ์™„๋ฃŒํ•˜์„ธ์š”.

    const initialValue = 0;
    const arrayToReduce = [1, 2, 3, 4, 5];
    arrayToReduce.reduce(
    (previousValue, currentValue) => {
    
        console.log("prev:", previousValue, "curr:", currentValue); 
        const result = previousValue + currentValue; 
        console.log("result", result); 
    
        return result
      },
      initialValue
    );
    


    ์ด๊ฒƒ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

    > "prev: " 0 "curr:" 1
    > "result" 1
    > "prev: " 1 "curr:" 2
    > "result" 3
    > "prev: " 3 "curr:" 3
    > "result" 6
    > "prev: " 6 "curr:" 4
    > "result" 10
    > "prev: " 10 "curr:" 5
    > "result" 15
    > 15
    


    ๋‚˜๋Š” ์ถœ๋ ฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์—†์ด ์ด๊ฒƒ์„ ์„ค๋ช…ํ•˜๊ณ  ๋ชจ๋“  ๊ฒƒ์ด ์†”์งํ•˜์ง€ ์•Š๋‹ค๊ณ  ๋Š๋‚๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ result๋Š” ์ฝœ๋ฐฑ( previousValue )์˜ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ๋ฐ˜๋ณต์— ์ถ”๊ฐ€๋˜๋ฉฐ ๋ฐฐ์—ด์ด ๋ฐ˜๋ณต๋˜๋Š” ํ˜„์žฌ ๋ฐ˜๋ณต์€ currentValue ์ž…๋‹ˆ๋‹ค.

    ์ด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

    const arrayToForLoop = [1, 2, 3, 4, 5];
    let valueToKeepTrackOf = 0; //equivalent to reduce's "initialValue" parameter
    let finalResult = [];
    
    arrayToForLoop.forEach(item => {
      console.log("prev:", valueToKeepTrackOf, "current:", item);
      valueToKeepTrackOf += item;
      console.log(valueToKeepTrackOf); 
      finalResult.push(valueToKeepTrackOf);
    });
    
    console.log(finalResult);
    


    ๊ทธ๋ž˜์„œ ๋‹น์‹ ์€ ๊ทธ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ์š”์ปจ๋Œ€ reduce์ž…๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž๋ฅผ ์ถ”๊ฐ€/์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค!

    ๋‚ด ๊ฐœ์ธ์ ์ธ ์กฐ์–ธ: ๋ณต์žกํ•œ ์ผ์— ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ์ •๋ง ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ JSON ๊ฐœ์ฒด์— ์ค‘์ฒฉ๋œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋จผ์ € ๋‹จ์ˆœํ™”ํ•˜๊ณ  ๊ฑฐ๊ธฐ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค.

    ์–ธ์ œ๋‚˜์ฒ˜๋Ÿผ MDN is a great source to understand this in its entirety .

    ํ‘œ์ง€ ์‚ฌ์ง„ ํฌ๋ ˆ๋”ง: La Cucina Italia

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ