반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

taenyLog

[JavaScript] Destructuring 객체 분해 본문

Web

[JavaScript] Destructuring 객체 분해

taenyLog 2023. 9. 26. 11:14
반응형

순서를 따르지 않는다. 배열 구조 분해보다 실용적임. 

 

사용자 정보 중 몇가지 특성에 접근하거나 선정해야 하는 경우 유용

// 예제 객체
const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
  city: 'New York'
};

// 객체 분해
const { firstName, lastName, age, city } = person;

// 분해한 값 출력
console.log('나이:', age);
console.log('도시:', city);
console.log('이름:', firstName);
console.log('성:', lastName);


// 변수 이름 변경
const {city : country} = person;

> country
< New York

 

 

디폴트 값 추가

const {city, state ='NaN'} = person;

state 값이 없다면 디폴트값인 NaN으로 된다.

반응형