codestates / codestates/DDO_Jurimma

[๐Ÿ”ง Front-End Error] #4 / Reduce of empty array / 2021-09-27

Open
#232 0 comments 0 reactions 2 assignees Claimed by @NAPP4287 View on GitHub
Error Front-End
Dominant language
JavaScript
Stars
4
Forks
1
PR merge metrics
No merged PRs in 30d

Description

# ์–ด๋–ค ์—๋Ÿฌ์ธ๊ฐ€์š”?

- ์Šคํ† ์–ด์—์„œ ๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์™€ map, reduceํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ธฐ ํŽธํ•˜๋„๋ก ๊ฐ€๊ณตํ•˜๊ณ  ์žˆ์—ˆ์Œ
- reduce๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ

# ์—๋Ÿฌ ๋ฉ”์‹œ์ง€

- `TypeError : Reduce of empty array with no initial value`

# ์—๋Ÿฌ ํ•ธ๋“ค๋ง ๋ฐฉ๋ฒ•

- reduce๋Š” ์ด 4๊ฐœ์˜ ์ธ์ž (๋ˆ„์ ๋˜๋Š” ๊ฐ’ acc, ํ˜„์žฌ ๊ฐ’ cur, ํ˜„์žฌ ์ธ๋ฑ์Šค idx, ์›๋ณธ ๋ฐฐ์—ด src)๋ฅผ ๊ฐ€์ง€๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ์ดˆ๊ธฐ๊ฐ’์„ ์‚ฌ์šฉํ•จ. ๋งŒ์•ฝ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์›๋ณธ ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ฒŒ ๋จ.
- ๋ฐฐ์—ด์— ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋‚˜, filter๋‚˜ map์„ ๊ฑฐ์ณ ๋งŒ์•ฝ ๋ฐฐ์—ด์— ์•„๋ฌด ๊ฐ’๋„ ๋‚จ์•„์žˆ์ง€ ์•Š์€ ์ƒํƒœ์—์„œ reduce๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋‚˜์˜ค๋Š” ์—๋Ÿฌ์˜€์Œ
- ๊ทธ๋ž˜์„œ reduceํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ 0์„ ์„ค์ •ํ•ด์ฃผ๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ์Œ.
- `array.reduce((acc,cur)=> { return .... }, 0)`

# ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ์œ„ํ•ด ์ฐธ๊ณ ํ•œ ๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ

[TypeError: Reduce of empty array with no initial value](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Errors/Reduce_of_empty_array_with_no_initial_value)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.