바르고 뜨겁게

[자바스크립트] 오브젝트에서 변수 꺼내기 (비구조화 할당 Object Destructuring) 본문

자바스크립트/Vanilla Js

[자바스크립트] 오브젝트에서 변수 꺼내기 (비구조화 할당 Object Destructuring)

RightHot 2019. 5. 17. 01:26



오브젝트에서 변수꺼내기 (비구조화 할당 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);
 


Comments