배열이나 객체의 속성을 해체하여
그 값을 개별 변수에 담을 수 있게 하는 JS 표현식❗
let obj = {
accessory: 'horn',
animal: 'horse',
color: 'purple',
hairType: 'curly'
}
☝
function buildAnimal(animalData) {
let accessory = animalData.accessory,
animal = animalData.animal,
color = animalData.color,
hairType = animalData.hairType;
...
} // 위에 객체에 접근하려면 이렇게 해야함,,,😬
👇
function buildAnimal(animalData) {
let {accessory, animal, color, hairType} = animalData;
...
} // 이렇게 하면 훨씬 간결해짐
let person = {
name: 'Maya',
age: 30,
phone: '123',
address: {
zipcode: 1234,
street: 'rainbow',
number: 42
}
}
👇
// 구조 분해 할당
let {address: {zipcode, street, number}} = person;
console.log(zipcode, street, number); // 1234 rainbow 42


const numbers = [1, 2, 3, 4, 5, 6];
const [,,three,,five] = numbers;

var people = [
{
name: "Mike Smith",
family: {
mother: "Jane Smith",
father: "Harry Smith",
sister: "Samantha Smith"
},
age: 35
},
{
name: "Tom Jones",
family: {
mother: "Norah Jones",
father: "Richard Jones",
brother: "Howard Jones"
},
age: 25
}
];
// name이라는 이름을 n으로 사용하겠다는 것
for (var {name: n, family: { father: f } } of people) {
console.log("Name: " + n + ", Father: " + f);
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"