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으로 된다.
반응형