Javascriptで任意の変数名に分割代入する

変数名が重複する場合とか変数名を変えて分割代入したいときは以下でできる。

const data = {
  month: '10', year: '2020'
}

const { month: thisMonth, year: thisYear } = data;

console.log({ thisMonth, thisYear });

さらに初期値も設定できる。

const response = {
  data: [
    1, 2, 3, 4, 5
  ]
}

const { data: array = []} = response;
console.log(array);

ちなみにパッケージからインポートするときの名前を変えるのはasだったりする。

import { Menu as MenuIcon } from '@material-ui/icons'

特種な演算子とかこういうのとか、ググらビリティが良くないので忘れると困る。