JavaScript

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋ฐฐ์—ด(array)์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ธ๊ธฐ์žˆ๋Š” ๋ฉ”์†Œ๋“œ map(), forEach() ๊ฐœ๋…๊ณผ ์ฐจ์ด์ 

martinooo 2024. 6. 24. 15:49

 

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œ


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ˜„์žฌ ์›น/์•ฑ ๊ฐœ๋ฐœ ํŠธ๋ Œ๋“œ์—์„œ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ ์•„์ดํ…œ์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฐ์—ด๊ณผ ์ด๋ฅผ ํŒŒ์‹ฑํ•˜๋ฉฐ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅํ•จ์ˆ˜์ธ map(), forEach()๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์ธ๊ธฐ ์žˆ๋Š” ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ์•Œ๊ณ  ๊ณ„์‹ค ๊ฒ๋‹ˆ๋‹ค.

์ด ๋‘ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์— ๋ฃจํ”„๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์ด ๋น„์Šทํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์ฐจ์ด์ ์„ ๊ณต๋ถ€ํ•˜์—ฌ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

๐Ÿคท‍โ™‚๏ธ ๋ฐฐ์—ด(array)์ด๋ž€?


๋ฐฐ์—ด์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์•„์ดํ…œ์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ, ์ถ”๊ฐ€, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

๋ฐฐ์—ด์€ ๊ฐ ์š”์†Œ์— ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„์ดํ…œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐฐ์—ด์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์ดํ•ดํ•˜๋ฉด

๐Ÿ’ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐฐ์—ด(array)์€ ์ด๋ฆ„๊ณผ ์ธ๋ฑ์Šค๋กœ ์ฐธ์กฐ๋˜๋Š” ์ •๋ ฌ๋œ ๊ฐ’์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค.
๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ ๊ฐ’์„ ๋ฐฐ์—ด ์š”์†Œ(element)๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ฐฐ์—ด์—์„œ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋ฅผ ์ธ๋ฑ์Šค(index)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

1. Javascript ๋ฐฐ์—ด์˜ map()์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

map() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋กœ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ์ƒˆ๋กœ์šด ๊ฐ’๋“ค์€ map() ๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ์ƒˆ ๋ฐฐ์—ด๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ ๊ฒƒ์€ map() ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ๊ธฐ์กด ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

 

javascript map() ํ•จ์ˆ˜ ์•Œ์•„๋ณด์ž!

https://cometruedream.tistory.com/7

 

javascript map() ํ•จ์ˆ˜ ์•Œ์•„๋ณด์ž

๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š” ๋ฐฐ์—ด์—์„œ ๋ชจ๋“  ๊ฐ’์„ ๊บผ๋‚ด๋Š” ๋ฐฉ์‹์€ ๋‹ค์–‘ํ•˜๊ฒŒ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค ๋Œ€ํ‘œ์ ์œผ๋กœ for๋ฌธ,,,ํ•˜์ง€๋งŒ ์˜ค๋Š˜์€ map() ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค  vs์ฝ”๋“œ๋ฅผ ์—ฌ์‹œ๊ณ  let getStartedarr ๋ฐฐ์—ด์—

cometruedream.tistory.com

 

2. Javascript ๋ฐฐ์—ด์˜ forEach()์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

forEach() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜์–ด ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๊ณ  ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ ๊ฒƒ์€ forEach() ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

 

javascript forEach() ํ•จ์ˆ˜ ์•Œ์•„๋ณด์ž!

https://cometruedream.tistory.com/3

 

JavaScript Foreach ์•Œ์•„๋ณด์ž!

์•ˆ๋…•ํ•˜์„ธ์š”!์˜ค๋Š˜์€ Foreach ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ForEach ๋ฉ”์„œ๋“œ๋Š” ๋ฌด์—‡์ธ๊ฐ€? forEach๋Š” ๋ฐฐ์—ด์— ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ๋กœ, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ์–ด์ง„ ๊ฐ’์„ ๋ฐฐ์—ด์š”์†Œ๋กœ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ˜•ํƒœ์ž…

cometruedream.tistory.com

 

map()๊ณผ forEach()์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€?

forEach()์™€ map()์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

์ด๋ฏธ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด map()์€ ๊ธฐ์กด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์— forEach()๋Š” ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿคท‍โ™‚๏ธ map()๊ณผ forEach() ๋น„๊ต ์˜ˆ์ œ


์ด๊ฒƒ์€ map()๊ณผ forEach() ๋ฉ”์†Œ๋“œ๋ฅผ ๋น„๊ตํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

// ๋ฐฐ์—ด ์„ ์–ธ
const numbers = [1, 2, 3, 4, 5];

// map() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์˜ˆ์ œ
const doubledNumbers = numbers.map((num) => {
  return num * 2;
});

// forEach() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์˜ˆ์ œ
numbers.forEach((num) => {
  console.log(num * 2);
});

console.log(doubledNumbers);

 

์œ„ ์˜ˆ์ œ์—์„œ map() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— 2๋ฅผ ๊ณฑํ•˜์—ฌ ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์—, forEach() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— 2๋ฅผ ๊ณฑํ•˜๊ณ  console.log()๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, map() ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ, doubledNumbers ์ƒ์ˆ˜์— ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์—, forEach() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์›๋ž˜ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜๋ฉฐ, ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ doubledNumbers ์ƒ์ˆ˜์— ๋ฐ˜ํ™˜๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

๐Ÿคท‍โ™‚๏ธ map()๊ณผ forEach()์˜ ํ™œ์šฉ๋ฒ•


map() ๋ฉ”์†Œ๋“œ ํ™œ์šฉ๋ฒ•

const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map((num) => {
  return num * num;
});

console.log(squaredNumbers); // [1, 4, 9, 16, 25]

 

forEach() ๋ฉ”์†Œ๋“œ ํ™œ์šฉ๋ฒ•

const numbers = [1, 2, 3, 4, 5];
let sum = 0;

numbers.forEach((num) => {
  sum += num;
});

console.log(sum); // 15

 

์œ„ ๋‘ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด map()๊ณผ forEach() ๋ฉ”์†Œ๋“œ์˜ ์ฐจ์ด์™€ ์‚ฌ์šฉ๋ฒ•์„ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

์žฌ๋ฐŒ๋Š” ์˜ค๋Š˜์˜ ๋ ๋ณ„ ์šด์„ธ ๋ณด๊ณ  ๊ฐ€์„ธ์š”! 

 

 

https://cometruedream.tistory.com/247

 

[React] Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผ className ์ปค์Šคํ…€

๐Ÿคท‍โ™‚๏ธ Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘ ๋ฐฐ๋„ˆ ๋งํฌ์— URL ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ 3์ค„ ํšจ๊ณผ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. React + Tailwind CSS ์…‹ํŒ…๋œ ํ”„๋กœ์ ํŠธ ์—ฌ์„œ CSS ํŒŒ์ผ & style ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/243

 

NestJS Lifecycle (์ƒ๋ช…์ฃผ๊ธฐ) ๊ฐœ๋…

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œ Nest JS ๊ฐœ๋…?Nest JS๋Š” Node JS ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ƒ๋‹นํžˆ ์˜ค๋žซ๋™์•ˆ Express ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์—์„œ ์••๋„์ ์ธ ์ 

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/242

 

๋ฆฌ์•กํŠธ Vite Proxy ํ™œ์šฉํ•œ CORS ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋ก ํŠธ์—”๋“œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ต์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ๊ธ€์„ ๋ณด์‹œ๋ฉด ๋” ๋งŽ์€ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค^^ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ตhttps://comet

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/241

 

React ํด๋”๊ตฌ์กฐ FSD ๊ธฐ๋Šฅ ๋ถ„ํ™œ ์„ค๊ณ„ ์•„ํ‚คํ…์ฒ˜

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ํด๋” ๊ตฌ์กฐ, ์„ค๊ณ„์— ๋Œ€ํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ์–ด๋ ต๋‹ค. ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์— React FSD ํด๋”๊ตฌ์กฐ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ค๊ณ„ํ•  ์˜ˆ์ •์ด๋ผ์„œ FSD ์•„ํ‚คํ…

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/244

 

์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋Š” Prisma ๋ช…๋ น์–ด ์ •๋ฆฌ

๐Ÿคท‍โ™‚๏ธ Prisma ๋ช…๋ น์–ด ๋ณธ๋ก ์— ์•ž์„œ Prisma๋ž€?Nest JS ํ”„๋ ˆ์ž„์›Œํฌ์™€ Prisma ORM์„ ํ™œ์šฉํ•ด์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ Prisma๋Š” Node JS ๊ธฐ๋ฐ˜์— ํ”„๋ ˆ์ž„์›Œํฌ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ƒํ˜ธ์ž‘์šฉ์ด ์ž˜๋˜๋ฉฐ Prisma + DB ์—ฐ๋™์œผ

cometruedream.tistory.com