자바스크립트 비 구조화 할당

OUO·2022년 3월 20일
0
post-thumbnail
let arr = ["one", "two", "three"];

let [one, two, three] = arr; //arr의 0번째는 one이라는 변수, 1번째는 two라는 변수, 2번째는 three라는 변수에 할당
console.log(one, two, three);

❗ 대괄호를 이용해 배열의 순서를 순서대로 할당받아 사용할 수 있는 방법 => 배열의 비 구조화 할당, 배열의 기본변수 비 구조화 할당

let [one, two, three] = ["one", "two", "three"];
console.log(one, two, three);

❗ 배열을 선언 자체에서 분리 => 배열의 선언 분리 비 구조화 할당

let [one, two, three, four = "four"] = ["one", "two", "three"];
console.log(one, two, three, four);

❗ 기본값을 설정하면 할당받지 못하는 상황에 변수의 기본값을 지정해줄수 있음

let a = 10;
let b = 20;

[a, b] = [b, a];
console.log(a, b);

❗ swift

let object = { one: "one", two: "two", three: "three", name: "김태주" };

let { name: myname, one, two, three, abc = "four" } = object;
console.log(one, two, three, myname, abc);

❗ 객체의 비 구조화 할당은 배열의 인덱스(순서)가 아니라 key 값을 기준으로 할당

profile
develoops!er

0개의 댓글