map
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ํจ์๋ฅผ ํธ์ถํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๋ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // ์ถ๋ ฅ: [2, 4, 6, 8]
forEach
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ. ๋ฐํ ๊ฐ์ด ์์(undefined).
const numbers = [1, 2, 3, 4];
numbers.forEach((num, index) => { console.log(`Element at index ${index}: ${num}`); });
filter
์ฃผ์ด์ง ํจ์์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๋ค๋ก ์ด๋ฃจ์ด์ง ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
const numbers = [1, 2, 3, 4];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // ์ถ๋ ฅ: [2, 4]
- map: ๋ณํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ ๋ฐฐ์ด๋ก ๋ฐํ.
- forEach: ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ํจ์๋ฅผ ์คํํ๋ฉฐ, ๋ฐํ ๊ฐ์ด ์์.
- filter: ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๋ค๋ก ์ด๋ฃจ์ด์ง ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ.
'์ฝ๋ฉ ๋ฉ๋ชจ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[error] VSCode์์ ํ์ผ ์์ ์ ์๋ฌ : Insufficient permissions. Select 'Retry as Sudo' to retry as superuser. (0) | 2024.01.06 |
---|---|
[error] ๋งฅ npm global ์ค์น์ Permission Error ( + command not found: npm) (0) | 2024.01.06 |
<select>์์ ์ ํ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ : HTMLCollection (0) | 2024.01.04 |
[error] bebel dependencies ์ค๋ฅ (0) | 2023.10.03 |
[javascript] padStart, padEnd (0) | 2023.10.02 |