JS

230112 JS ES6 JSON에서의 Destructuring

주영재 2023. 1. 12. 18:26
//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;

        //두번째 객체의 num,title
        let [, { num, title }] = list;
        console.log(num, title);

        //두번째 객체의 info
        let [, { info }] = list;
        console.log(info);

        //두번째 객체의 profile
        let [, { profile }] = list;
        console.log(profile);
        console.log(profile.birth);

        //두번째 객체의 info를 다른이름으로 가지고 나오기
        let [, { info: x }] = list;
        console.log(x);

        //두번째 객체의 info배열의 첫번째 요소만
        let [, { info: [y1] }] = list;
        console.log(y1);

        //두번째 객체의 profile안에 있는 birth값
        let [, { profile: { birth:xcxx } }] = list;
        console.log(xcxx);

몇번째 객체에서 정보를 뽑을지에 따라 적는 구문이 바뀌는 걸 확인하기.