JS | ES6 문법 - Destructuring assignment (구조 분해 할당)

2023. 1. 12. 16:03·JavaScript

Destructuring assignment (구조 분해 할당)

1. 배열에서 destructuring

let arr = [1,2,3,4];
// let a1 = arr[0];
// let a2 = arr[1];

let [a, b, c, d] = arr;
console.log(a, b, c, d); //1 2 3 4

let [a1, a2] = arr;
console.log(a1, a2); //1, 2

let [k1, , ,k4] = arr;
console.log(k1,k4); //1, 4

let [y1, ...y2] = arr;
console.log(y1, y2); //1, [2, 3, 4]

let [...ar] = arr;
console.log(ar); //[1, 2, 3, 4]

function useState() {
      return [1, function(){}];
}

let result = useState();
console.log(result); //[1, ƒ] //두번째값은 function

let [n1,n2] = useState();
console.log(n1,n2); //1, function

2. 객체 Destructuring

 let obj = {name : "홍길동", age :20};
        // console.log(obj.name);
        // console.log(obj["name"]);

        // let {name} = obj;
        // console.log(name);

        // let {name,age} = obj;
        // console.log(name,age);

        // let {...arr} = obj;
        // console.log(arr);  //객체를 통째로 갖고 나옴

        let person = {
            name : "홍길동",
            age : 30,
            job : ["js", "css"],
            info : {id : "aaa123"}
        };

        // let {name, job} = person;
        // console.log(name,job);

        // let {name, job, info} = person; 
        // console.log(name,job,info);

        //객체의 값을 다른 이름으로 가지고 나오기 
        let {name : aaa} = person; //name의 값을 aaa로 받을거다 
        console.log(aaa);

        let{name:nnn, job:jjj} = person;
        console.log(nnn,jjj); //홍길동, ['js', 'css']

3. 제이슨 Destructuring

 //JSON형식에서 디스트럭쳐링
        let list = [
            {num : 1, title : "hello", info : [1,2,3], profile : {birth : "2023"}},
            {num : 2, title : "bye", info : [4,5,6], profile : {birth : "2022"}}
        ]

        let[a, b] = list;
        console.log(b);
        //{num : 2, title : "bye", info : [4,5,6], profile : {birth : "2022"}}

        //두번째 객체의 num, title
        let [, {num, title}] = list;
        console.log(num,title); //2 'bye'
        //두번째 객체의 info
        let [, {info}] = list;
        console.log(info); //[4, 5, 6]
        //두번째 객체의 profile
        let [, {profile}] = list;
        console.log(profile); //{birth: '2022'}
        //두번째 객체의 info를 다른이름으로 가지고 나오기
        let[, {info:x}] = list; //x는 배열
        console.log(x); // [4, 5, 6]
        //두번째 객체의 info배열의 첫번째 요소만
        let[,{info:[y1]}] = list;
        console.log(y1); //4
        //두번째 객체의 profile안에 들어있는 birth값
        let [,{profile:{birth}}] = list; 
        console.log(birth);//2022

'JavaScript' 카테고리의 다른 글

JS | ES6 문법 - for of 문  (0) 2023.01.12
JS | ES6 문법 - backtick  (0) 2023.01.12
JS | ES6 문법 - spread operator (전개구문)  (0) 2023.01.12
JS | ES6 문법 - let 변수, const 변수  (0) 2023.01.12
JS | AJAX (비동기 통신)  (0) 2023.01.06
'JavaScript' 카테고리의 다른 글
  • JS | ES6 문법 - for of 문
  • JS | ES6 문법 - backtick
  • JS | ES6 문법 - spread operator (전개구문)
  • JS | ES6 문법 - let 변수, const 변수
개발 공부
개발 공부
  • 개발 공부
    개발 공부
    개발 공부
  • 전체
    오늘
    어제
    • 전체보기 (163)
      • 프로젝트 (1)
      • JavaScript (46)
      • Node.js (3)
      • Next.js (5)
      • React (17)
      • NoSQL (0)
      • HTML,CSS (3)
      • CS (6)
      • Java (35)
      • Spring (6)
        • Spring의 정석 (1)
      • Spring boot (1)
      • MySQL (1)
      • 리눅스 (16)
      • JSP (9)
      • AWS (0)
      • git (2)
      • 알고리즘 (1)
      • ect (7)
      • Project (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자바
    티스토리챌린지
    Java
    자바의 정석
    Java의 정석
    오블완
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
개발 공부
JS | ES6 문법 - Destructuring assignment (구조 분해 할당)
상단으로

티스토리툴바