JS ๐Ÿ”ฅ: ๊ฐœ์ฒด ์†์„ฑ์„ ์กฐ๊ฑด๋ถ€๋กœ ์„ค์ •

3690 ๋‹จ์–ด tutorialwebdevjavascriptbeginners
์›น ๊ฐœ๋ฐœ ๐Ÿš€๐Ÿš€๐Ÿš€์„ ๋” ์ž˜ํ•˜๊ณ  ์‹ถ์œผ์„ธ์š”? https://codesnacks.net/subscribe/์—์„œ ๋‚ด ์ฃผ๊ฐ„ ๋‰ด์Šค๋ ˆํ„ฐ๋ฅผ ๊ตฌ๋…ํ•˜์‹ญ์‹œ์˜ค.


์–ด๋–ค ์กฐ๊ฑด์ด ์ฐธ์ธ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ์ฒด ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๋ฌผ๋ก  if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const someCondition = true;
const anotherCondition = false;
const myObject = {
  name: "codesnacks",
};

if(someCondition){
  myObject.author = "Ben";
}

if(anotherCondition){
  myObject.platform = "dev.to";
}

console.log(myObject); // {name: "codesnacks", author: "Ben"}

๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ์กฐ๊ฑด๊ณผ ํ•จ๊ป˜ ๊ฐ์ฒด ํ™•์‚ฐ ์—ฐ์‚ฐ์ž( ... )๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ if ๋ฌธ์€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐœ์ฒด์— ์—ฌ๋Ÿฌ ์กฐ๊ฑด๋ถ€ ์†์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

const someCondition = true;
const anotherCondition = false;
const myObject = {
  name: "codesnacks",
  ...(someCondition && { author: "Ben" }),
  ...(anotherCondition && { platform: "dev.to" }),
};

console.log(myObject); // {name: "codesnacks", author: "Ben"}



์›น ๊ฐœ๋ฐœ์„ ๋” ์ž˜ํ•˜๊ณ  ์‹ถ์œผ์‹ญ๋‹ˆ๊นŒ?
๐Ÿš€๐Ÿš€๐Ÿš€subscribe to the Tutorial Tuesday โœ‰๏ธnewsletter

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ