๐น some() ๋ฉ์๋๋?
๐ ๋ฐฐ์ด์ ์์ ์ค ํ๋๋ผ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true ๋ฐํ
๐ ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ผ๋ฉด false ๋ฐํ
๐ filter()๋ map()๋ณด๋ค ๋น ๋ฅด๊ฒ ๊ฒ์ฌ ๊ฐ๋ฅ (ํ๋๋ง ์ฐพ์ผ๋ฉด ๋ฐ๋ก ์ข
๋ฃ๋จ)
โ some() ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
let numbers = [10, 20, 30, 40, 50];
let hasBigNumber = numbers.some(num => num > 30);
console.log(hasBigNumber); // true (40๊ณผ 50์ด ์์)
let hasNegative = numbers.some(num => num < 0);
console.log(hasNegative); // false (์์๊ฐ ์์)
โ some()์ ์ฌ์ฉํ ์ ํจ์ฑ ๊ฒ์ฌ
์๋ ์ฝ๋์์ some()์ ์ฌ์ฉํด ์ ๋ ฅ๊ฐ์ด 100์ ์ด๊ณผํ๊ฑฐ๋ ์๋ชป๋ ๊ฐ์ธ์ง ํ์ธํ์ด์.
let scores = [90, 105, 80];
if (scores.some(score => isNaN(score) || score < 0 || score > 100)) {
console.log('โ ์ฌ๋ฐ๋ฅธ ์ ์๋ฅผ ์
๋ ฅํ์ธ์!');
} else {
console.log('โ
์
๋ ฅ๋ ์ ์๋ ์ ์์
๋๋ค.');
}
๐ ๋์ ๋ฐฉ์
- some()์ด scores ๋ฐฐ์ด์ ๋๋ฉด์ score > 100 ๋๋ score < 0 ๋๋ isNaN(score) ์กฐ๊ฑด์ ๊ฒ์ฌ
- 105๊ฐ score > 100์ ๋ง์กฑํ๋ฏ๋ก ์ฆ์ true ๋ฐํ ํ ์ข ๋ฃ
- "โ ์ฌ๋ฐ๋ฅธ ์ ์๋ฅผ ์ ๋ ฅํ์ธ์!" ์ถ๋ ฅ
๐ฏ some() vs every() ์ฐจ์ด
- some() → ํ๋๋ผ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true
- every() → ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํด์ผ true
let scores = [90, 80, 70];
console.log(scores.some(score => score > 100)); // false (100 ์ด๊ณผ ์์)
console.log(scores.every(score => score < 100)); // true (๋ชจ๋ ๊ฐ์ด 100 ๋ฏธ๋ง)
๐ฅ some()์ ์จ์ผ ํ๋ ๊ฒฝ์ฐ
โ
๋ฐฐ์ด์ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๊ฐ ์๋์ง ๋น ๋ฅด๊ฒ ๊ฒ์ฌํ ๋
โ
๋ชจ๋ ์์๋ฅผ ๊ฒ์ฌํ ํ์ ์์ด ํ๋๋ผ๋ ๋ฐ๊ฒฌ๋๋ฉด ๋ฐ๋ก ๋ฉ์ถ ๋
โ
์ ํจ์ฑ ๊ฒ์ฌ(์ซ์ ์ฒดํฌ, ๋ฒ์ ์ฒดํฌ, ์ค๋ณต ์ฒดํฌ) ๋ฑ์ ํ์ฉํ ๋
๐น reduce()๋?
๐ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ํ๋์ ๊ฐ์ผ๋ก ์ค์ด๋(reduce) ๋ฉ์๋
๐ ๋์ (accumulator) ๊ฐ๊ณผ ํ์ฌ(current) ๊ฐ์ ์ด์ฉํ์ฌ ์ฐ์ฐ
๐ ํฉ๊ณ, ํ๊ท , ์ต๋๊ฐ, ์ต์๊ฐ, ๊ฐ์ฒด ๋ณํ ๋ฑ ๋ค์ํ ์์
๊ฐ๋ฅ
โ reduce() ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
let numbers = [10, 20, 30, 40];
let sum = numbers.reduce((acc, cur) => acc + cur, 0);
console.log(sum); // 100
๐ ๋์ ๋ฐฉ์ (reduce() ๋ด๋ถ ์คํ ๊ณผ์ )
Iteration | acc (๋์ ๊ฐ) | cur (ํ์ฌ๊ฐ) | ์ฐ์ฐ ๊ฒฐ๊ณผ |
์ด๊ธฐ๊ฐ | 0 | 10 | 0 + 10 = 10 |
1๋ฒ์งธ | 10 | 20 | 10 + 20 = 30 |
2๋ฒ์งธ | 30 | 30 | 30 + 30 = 60 |
3๋ฒ์งธ | 60 | 40 | 60 + 40 = 100 |
๐ reduce((acc, cur) => acc + cur, 0)
- acc → ๋์ ๊ฐ (์ด์ ์ฐ์ฐ ๊ฒฐ๊ณผ)
- cur → ํ์ฌ ๋ฐฐ์ด ์์
- 0 → ์ด๊ธฐ๊ฐ (์๋ตํ๋ฉด ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๊ฐ ์ด๊ธฐ๊ฐ)
๐ฏ reduce()๋ฅผ ํ์ฉํ ๋ค์ํ ์์
1๏ธโฃ ๋ฐฐ์ด ์์ ํฉ ๊ตฌํ๊ธฐ
let numbers = [5, 10, 15];
let total = numbers.reduce((acc, cur) => acc + cur, 0);
console.log(total); // 30
2๏ธโฃ ๋ฐฐ์ด ์์ ํ๊ท ๊ตฌํ๊ธฐ
let scores = [80, 90, 100];
let average = scores.reduce((acc, cur) => acc + cur, 0) / scores.length;
console.log(average); // 90
3๏ธโฃ ์ต๋๊ฐ & ์ต์๊ฐ ๊ตฌํ๊ธฐ
let numbers = [25, 78, 3, 99, 50];
let max = numbers.reduce((acc, cur) => (acc > cur ? acc : cur), numbers[0]);
console.log(max); // 99
let min = numbers.reduce((acc, cur) => (acc < cur ? acc : cur), numbers[0]);
console.log(min); // 3
4๏ธโฃ ๋ฐฐ์ด ์์ ๊ฐ์ ์ธ๊ธฐ (๊ฐ์ฒด ๋ณํ)
let fruits = ['๐', '๐', '๐', '๐', '๐', '๐'];
let count = fruits.reduce((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1; // ํ์ฌ ๊ณผ์ผ ๊ฐ์ ์นด์ดํธ
return acc;
}, {});
console.log(count); // { '๐': 3, '๐': 2, '๐': 1 }
*** ์ด๊ฑฐ ์ฝ๋๊ฐ ์ด๋ ค์ฐ๋ ์ข ๋... ์์ธํ ํ์ด๋ณด์!
1. ๋ฐฐ์ด์ reduce()๋ก ์ํํ๋ฉด์ ๊ฐ์ ์ ์ฅ
let count = fruits.reduce((acc, cur) => { ... }, {});
- acc → ๋์ ๊ฐ์ฒด (๊ณผ์ผ ๊ฐ์๋ฅผ ์ ์ฅํ ๊ฐ์ฒด)
- cur → ํ์ฌ ์ํ ์ค์ธ ๊ณผ์ผ (๐, ๐, ๋ฑ)
- {} → ์ด๊ธฐ๊ฐ์ผ๋ก ๋น ๊ฐ์ฒด {} ์ฌ์ฉ (๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๊ณต๊ฐ)
2. ๊ฐ ๊ณผ์ผ ๊ฐ์ ์ธ๊ธฐ
acc[cur] = (acc[cur] || 0) + 1;
์ด ๋ถ๋ถ์ด ํต์ฌ์ ๋๋ค! ๐ก
- acc[cur] → ํ์ฌ ๊ณผ์ผ(cur)์ ๊ฐ์๋ฅผ acc์์ ๊ฐ์ ธ์ด
- ๋ง์ฝ ์ฒซ ๋ฑ์ฅ์ด๋ผ๋ฉด acc[cur]์ด undefined
- undefined || 0 → 0์ผ๋ก ์ค์ (์ด๊ธฐ๊ฐ์ 0์ผ๋ก ์ค์ )
- (acc[cur] || 0) + 1 → ๊ธฐ์กด ๊ฐ์์์ +1 ์ฆ๊ฐ
- acc[cur] = ... → ์ฆ๊ฐ๋ ๊ฐ์๋ฅผ ๋ค์ ๊ฐ์ฒด์ ์ ์ฅ
๐ ์์ ๋ก ํ์ด๋ณด๊ธฐ
| cur ๊ฐ | ๊ธฐ์กด acc[cur] ๊ฐ | (acc[cur] || 0) + 1 | ์ต์ข acc ๊ฐ์ฒด | |----------|-------------------|---------------------|------------------| | ๐ | undefined (์์) | 0 + 1 = 1 | { '๐': 1 } | | ๐ | undefined (์์) | 0 + 1 = 1 | { '๐': 1, '๐': 1 } | | ๐ | 1 (์ด๋ฏธ ์์) | 1 + 1 = 2 | { '๐': 2, '๐': 1 } | | ๐ | undefined (์์) | 0 + 1 = 1 | { '๐': 2, '๐': 1, '๐': 1 } | | ๐ | 2 (์ด๋ฏธ ์์) | 2 + 1 = 3 | { '๐': 3, '๐': 1, '๐': 1 } | | ๐ | 1 (์ด๋ฏธ ์์) | 1 + 1 = 2 | { '๐': 3, '๐': 2, '๐': 1 } |
3. ๊ฒฐ๊ณผ ์ถ๋ ฅ
console.log(count);
// { '๐': 3, '๐': 2, '๐': 1 }
→ ๊ฐ ๊ณผ์ผ์ด ๋ช ๋ฒ ๋์๋์ง ๊ฐ์๋ฅผ ์ธ์ ๊ฐ์ฒด๋ก ๋ณํ ์๋ฃ! ๐ฏ
โจ ์ต์ข ์ ๋ฆฌ
โ reduce()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๊ฐ์ฒด๋ก ๋ณํ
โ (acc[cur] || 0) + 1 ์ ์ฌ์ฉํด ํ์ฌ ๊ฐ์ด ์์ผ๋ฉด 0์ผ๋ก ์ค์ ํ 1 ์ฆ๊ฐ
โ ๊ฐ์ฒด {}๋ฅผ ์ด๊ธฐ๊ฐ์ผ๋ก ์ค์ ํ๊ณ cur์ ํค(key)๋ก ์ฌ์ฉ
5๏ธโฃ ๊ฐ์ฒด ๋ฐฐ์ด์ ํฉ ๊ตฌํ๊ธฐ
let items = [
{ name: "์ฌ๊ณผ", price: 1000 },
{ name: "๋ฐ๋๋", price: 1500 },
{ name: "ํฌ๋", price: 2000 }
];
let totalPrice = items.reduce((acc, cur) => acc + cur.price, 0);
console.log(totalPrice); // 4500
๐ฅ reduce() vs forEach() vs map() ์ฐจ์ด
๋ฉ์๋ | ๋ชฉ์ | ๋ฐํ๊ฐ | ํน์ง |
forEach() | ๋จ์ ๋ฐ๋ณต ์คํ | ์์ (undefined) | break ๋ถ๊ฐ๋ฅ |
map() | ๋ฐฐ์ด ๋ณํ | ์๋ก์ด ๋ฐฐ์ด | ์๋ณธ ๋ฐฐ์ด ์ ์ง |
reduce() | ๊ฐ ์ถ์ | ์ต์ข ๊ฒฐ๊ณผ๊ฐ | ๋์ ์ฐ์ฐ ๊ฐ๋ฅ |
๐ ์ ๋ฆฌ
- reduce()๋ ๋ฐฐ์ด์ ํ๋์ ๊ฐ์ผ๋ก ์ค์ด๋ ๋ฉ์๋
- ํฉ๊ณ, ํ๊ท , ์ต๋/์ต์๊ฐ, ๊ฐ์ ์ธ๊ธฐ, ๊ฐ์ฒด ๋ณํ ๋ฑ ํ์ฉ ๊ฐ๋ฅ
- ๋์ ๊ฐ(acc)๊ณผ ํ์ฌ๊ฐ(cur)์ ์ด์ฉํ ๊ณ์ฐ ๊ตฌ์กฐ
- reduce()๋ฅผ ์ฌ์ฉํ๋ฉด for๋ฌธ๋ณด๋ค ๋ ๊ฐ๊ฒฐํ ์ฝ๋ ์์ฑ ๊ฐ๋ฅ!
*์ ํฌ์คํ ์ ์ฑGPT ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋์์ต๋๋ค!
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ์ฌ๊ท ๊ธฐ๋ฅ (0) | 2025.03.02 |
---|---|
[JS] ์ฝ๋ฉํ ์คํธ ๋๋น DFS(๊น์ด ์ฐ์ ํ์) & BFS(๋๋น ์ฐ์ ํ์) (0) | 2025.03.02 |
[JS] ์๋ฐ์คํฌ๋ฆฝํธ split() ๋ฉ์๋ (0) | 2025.02.19 |
[JS] ์๋ฐ์คํฌ๋ฆฝํธ์ ์กฐ๊ฑด๋ฌธ ์ ๋ฆฌ (0) | 2025.02.18 |
[JS] ์๋ฐ์คํฌ๋ฆฝํธ ๋ณ์ var , let, const ์ฐจ์ด ๊ทธ๋ฆฌ๊ณ ํธ์ด์คํ (0) | 2025.02.18 |