codestates / codestates/DDO_Jurimma
[๐ง Front-End Error] #4 / Reduce of empty array / 2021-09-27
- 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.