Destructuring

구조 분해 할당

객체는 property name, 배열은 index position으로 값을 꺼내며 alias, default, rest, nested 패턴을 함께 쓸 수 있다.

문법별 사용 지점

data extract

객체 분해

const { id, name: displayName } = user처럼 alias로 이름 충돌을 피한다.

배열 분해

const [first, , third] = list처럼 순서와 빈 칸으로 필요한 값만 고른다.

기본값

const { page = 1 } = query처럼 undefined일 때만 대체값을 넣는다.

나머지 값

const { password, ...safeUser } = user처럼 제외한 속성을 새 객체로 모은다.

sourcepickrenamerest
문법별 사용 지점

nested destructuring으로 깊은 email을 바로 꺼낼 수 있다. profile이 없을 수 있는 데이터는 optional chaining이 더 안전하다.