ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

'destucture'์˜ ๋œป์€ ํŒŒ๊ดดํ•˜๋‹ค

๋ฐฐ์—ด, ๊ฐ์ฒด ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„์“ฐ๋‹ค.

 

Destructuring

 

๐Ÿ“array ์ ์šฉ ์˜ˆ์‹œ

1. ์•„๋ž˜ ๋ฐฐ์—ด์—์„œ "๊ฐ•์•„์ง€"๋Š” dog๋ผ๋Š” ๋ณ€์ˆ˜์—,

"๊ณ ์–‘์ด"๋Š” cat์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์œผ๋ ค๋ฉด?

const ๋™๋ฌผ๋ฆฌ์ŠคํŠธ = ["๊ฐ•์•„์ง€", "๊ณ ์–‘์ด"];
๋”๋ณด๊ธฐ

์ •๋‹ต

1.

const dog = ๋™๋ฌผ๋ฆฌ์ŠคํŠธ[0]; // dog => "๊ฐ•์•„์ง€"
const cat = ๋™๋ฌผ๋ฆฌ์ŠคํŠธ[1]; // cat => "๊ณ ์–‘์ด"

 

2.

const ๋™๋ฌผ๋ฆฌ์ŠคํŠธ = ["๊ฐ•์•„์ง€", "๊ณ ์–‘์ด"];
const [dog, cat] = ๋™๋ฌผ๋ฆฌ์ŠคํŠธ;
// dog => "๊ฐ•์•„์ง€"
// cat => "๊ณ ์–‘์ด"

=> ์ˆœ์„œ์— ๋งž๊ฒŒ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น์ด ์ ์šฉ๋œ๋‹ค.

 

2. ๋ฐฐ์—ด colors์—์„œ ์„ธ ๋ฒˆ์งธ ๊ฐ’์ธ blue๋งŒ ์ถœ๋ ฅํ•˜๋ ค๋ฉด?

const colors = ["red", "green", "blue"];

console.log();

 

๋”๋ณด๊ธฐ

์ •๋‹ต

const colors = ["red", "green", "blue"];
const [, , thirdColor] = colors;

console.log(thirdColor); //blue ์ถœ๋ ฅ๋จ

 

3. ๋‹ค์Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ถœํ•˜์—ฌ firstthird ๋ณ€์ˆ˜์— ๋‹ด์•„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

const numbers = [10, 20, 30, 40, 50];
๋”๋ณด๊ธฐ

์ •๋‹ต

const [first, , third] = numbers;
console.log(first); // 10
console.log(third); // 30

 

4. ํ˜ธํ…”์˜ ์˜ˆ์•ฝ ํ™•์ธ ์‹œ์Šคํ…œ์˜ ํ•จ์ˆ˜๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ฒฐ๊ณผ์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.(๋ณ€์ˆ˜๋ช… ๋ฐ”๊พธ๊ธฐ)

function confirmReservation(user) {
        // ์—ฌ๊ธฐ์— user ๊ฐ์ฒด๋ฅผ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ํ•˜์„ธ์š”.
        
        return `${name} ๊ณ ๊ฐ๋‹˜์˜ ${roomType}๋ฃธ ์ž…์‹ค๋‚ ์งœ๋Š” ${firstDate} ์ž…๋‹ˆ๋‹ค.`
}

const userInfo = {
  name: "James",
  roomType: "Deluxe",
  date: "2023-05-30"
}
const result = confirmReservation(userInfo);
console.log(result);
๋”๋ณด๊ธฐ

์ •๋‹ต

function confirmReservation(user) {
        // ์—ฌ๊ธฐ์— user ๊ฐ์ฒด๋ฅผ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ํ•˜์„ธ์š”.
        const {name, roomType, date: firstDate} = user;
        
        return `${name} ๊ณ ๊ฐ๋‹˜์˜ ${roomType}๋ฃธ ์ž…์‹ค๋‚ ์งœ๋Š” ${firstDate} ์ž…๋‹ˆ๋‹ค.`
}

const userInfo = {
  name: "James",
  roomType: "Deluxe",
  date: "2023-05-30"
}
const result = confirmReservation(userInfo);
console.log(result); // ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 'James ๊ณ ๊ฐ๋‹˜์˜ Deluxe๋ฃธ ์ž…์‹ค๋‚ ์งœ๋Š” 2023-05-30 ์ž…๋‹ˆ๋‹ค.'

 

 

 

๐Ÿ“object ์ ์šฉ ์˜ˆ์‹œ

1. ์•„๋ž˜ ๊ฐ์ฒด์—์„œ "๋ฉ‹์ง„์…”์ธ "๋Š” '์ƒํ’ˆ๋ช…'์ด๋ผ๋Š” ๋ณ€์ˆ˜์—,

10000์€ '๊ฐ€๊ฒฉ'์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์œผ๋ ค๋ฉด?

const product = {
	์ƒํ’ˆ๋ช…: "๋ฉ‹์ง„์…”์ธ ",
	๊ฐ€๊ฒฉ: 10000,
}
๋”๋ณด๊ธฐ

์ •๋‹ต

1.

const ์ƒํ’ˆ๋ช… = product.์ƒํ’ˆ๋ช…;
const ๊ฐ€๊ฒฉ = product.๊ฐ€๊ฒฉ;

 

2.

const product = {
	์ƒํ’ˆ๋ช…: "๋ฉ‹์ง„์…”์ธ ",
	๊ฐ€๊ฒฉ: 10000,
}

const { ๊ฐ€๊ฒฉ, ์ƒํ’ˆ๋ช… } = product;

 

=> ๊ฐ์ฒด ๋‚ด๋ถ€ ์š”์†Œ์˜€๋˜ ๊ฒƒ์ด ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜์—ˆ๋‹ค.

 

2. person์˜ name๊ณผ age๋ฅผ greet ํ•จ์ˆ˜์— ์ ์šฉ์‹œ์ผœ์„œ

"์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ๋ฅด์ˆœ์ด์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 28์ด์—์š”." ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€?

function greet() {
	console.log()
}

const person = {
	name : "๋ฅด์ˆœ์ด",
	age: 28,
}

greet(person);
๋”๋ณด๊ธฐ

์ •๋‹ต

1.

function greet(person) {
	console.log(`์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ${person.name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” ${person.age}์ด์—์š”.`)
}

const person = {
	name : "๋ฅด์ˆœ์ด",
	age: 28,
}

greet(person);

 

2.

function greet({ name, age }) {
	console.log(`์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ${name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” ${age}์ด์—์š”.`)
}

const person = {
	name : "๋ฅด์ˆœ์ด",
	age: 28,
}

greet(person);

 => ๊ฐ์ฒด๋Š” key์˜ ์ด๋ฆ„์„ ๋”ฐ๋ผ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— greet ๋‚ด๋ถ€์˜ ์ธ์ž์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.

 

3. ๋‹ค์Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์—์„œ name๊ณผ age๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ถœํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

const person = {
  name: "๋ฅดํƒ„์ด",
  age: 25,
  job: "๊ฐœ๋ฐœ์ž"
};
๋”๋ณด๊ธฐ

์ •๋‹ต

const { name, age } = person;
console.log(name);  // ๋ฅดํƒ„์ด
console.log(age);   // 25

 

4. ๋‹ค์Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์—์„œ title๊ณผ year๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ถœํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.(์ค‘์ฒฉ ๊ฐ์ฒด)

const movie = {
  title: "Inception",
  director: "Christopher Nolan",
  release: {
    year: 2010,
    month: "July"
  }
};
๋”๋ณด๊ธฐ
์ •๋‹ต
const { title, release: { year } } = movie;
console.log(title); // Inception
console.log(year);  // 2010
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2026/03   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ