비 구조화 할당

Hoo·2023년 4월 11일
0

배열의 비구조화 할당

  • 비구조화할당이란 배열이나 객체의 속성 혹은 값을 해체하여 그 값을 변수에 각각 담아 사용하는 자바스크립트 표현식

    기존 배열 출력 코드

    let arr = ["one","two","three"];
    console.log(arr[0]);
    console.log(arr[1]);
    console.log(arr[2]);
    
    //결과값 : one, two, three

    비구조화 할당방식 배열 선언 및 호출

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

    변수의 값 스왑 바꾸기

    let a = 10;
     let b = 20;
    
    [a, b] = [b, a]
    console.log(a, b)
    
    //결과값 : 20 10

    객체의 비구조화 할당

    let obj = { one: "one", two: "two", three: "three" };
    let { one, two, three } = obj;
    console.log(one, two, three);
profile
기록하는중입니다.

0개의 댓글