바르고 뜨겁게
[자바스크립트] 오브젝트에서 변수 꺼내기 (비구조화 할당 Object Destructuring) 본문
오브젝트에서 변수꺼내기 (비구조화 할당 Object Destructuring)
1. 변수명 변경해서 꺼내기
2. 오브젝트 변수명으로 받
3. 배열 형태로 꺼내기
const obj = {
depth1_0: "1단계 0",
depth1_1: "1단계 1",
depth1_2: {
depth2_0: "2단계 0",
depth2_1: "2단계 1"
}
}
// 기존방식
const oldDepth1 = obj.depth1_0;
const oldDepth2 = obj.depth1_2.depth2_0;
console.log(`기존방식 oldDepth1: ${oldDepth1} / oldDepth2: ${oldDepth2}`);
// (비구조화 할당 Object Destructuring)
// 변수명변경 , 그대로받음 , 배열형태 추출
const {depth1_0: newDepth1, depth1_1, depth1_2: { depth2_0, depth2_1} } = obj;
console.log(newDepth1 , depth1_1 , depth1_2);
'자바스크립트 > Vanilla Js' 카테고리의 다른 글
Vanilla JS - Array.map (0) | 2019.05.21 |
---|---|
Vanilla JS - classes (자바스크립트 객체 지향 프로그래밍) (0) | 2019.05.21 |
Vanilla JS - async/await (0) | 2018.12.13 |
Vanilla JS - 콜백(callback)과 프로미스(promise) 비교 (0) | 2018.12.12 |
Vanilla JS - REST 문법 (배열) (0) | 2018.12.12 |
Comments