分割代入は構造変換に便利

let user = {};
[user.name, user.surname] = "John Smith".split(' ');

これは、

  1. 文字列 "John Smith"
  2. 配列 ["John", "Smith"]
  3. オブジェクト {name: "John", surname: "Smith"}

と変換している。

代入の左辺はロケーションパスを使ったロケーションパターン。右辺は構造の一部をリストとして抽出したもの。抽出〈select〉と割り当て〈assign〉を一度に行っている。