구조 분해 할당이란? 배열이나 객체의 속성을 해체하여 그 값을 개별 변수에 담을 수 있게 하는 JavaScript 표현식이다.
ex)
//예시
const player = {
name : "KAIN",
club : "tottenhamhotspur",
address:{
city: "tottenham"
}
}
console.log(player.address.city);// tottenham
const {name, club, address:{city}} = player // destructuring
console.log(name, club, address) // KAIN, tottenhamhotspur, tottenham
console.log(`${name} lives in ${city}!`)//KAIN lives in tottenham
ex)
let [firstName, middleName, lastName] = ["a", "b", "c"];
lastName = "gg";
console.log(firstName,middleName,lastName); // a, b, gg
먼저 배열을 만들어 firstName, middleName, lastName에 각각 “a”,” b”,” c”를 선언하고 lastName에 접근하여 lastName의 값은 “c”를 “gg”로 바꾼다
student에 있는 Object를 활용하여 “sam은 19 diceGame(Two plater dice game using JavaScript.)을 만들고 있다.” 출력하세요.
const student = {
name: "sam",
age: 19,
projects: {
diceGame: "Two plater dice game using JavaScript."
}
};
const {
name,
age,
projects: { diceGame }
} = student;
console.log(`${name}은 ${age} diceGame(${diceGame})을 만들고 있다`);