# destructuring array

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [operator](https://lochiwei.gitbook.io/web/js/grammar/op) ⟩ [assignment](https://lochiwei.gitbook.io/web/js/grammar/op/assign) ⟩ [destructuring](https://lochiwei.gitbook.io/web/js/grammar/op/assign/destruct) ⟩ array

{% hint style="success" %}

```javascript
let [a, b] = [1, 2, 3]
let [a, ...rest] = array
```

{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}
💾 程式：[replit](https://replit.com/@pegasusroe/JS-array-destructuring)

```javascript
// array destructuring
let [a, b] = ["John", "Smith"]
let [a, b] = "John Smith".split(' ')

let [a, , c] = ["hi","what","hello"];  // ⭐ ignore elements
let [a, b, ...rest] = "abcdef"         // ⭐ "rest" (array)
let [s, t = 0] = [1];                  // ⭐ default values

let [
    name = prompt('name?'),            // ⭐ return value as default
    surname = prompt('surname?')
] = ["Julius"];

// tricks

// ⭐ assign to object's properties
let user = {};
[user.name, user.surname] = "John Smith".split(' ');
// user = { name: 'John', surname: 'Smith' }

// ⭐ "swap" trick
let [p, q] = [1, 2];
[p, q] = [q, p];                     // p=2, q=1
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] JS.info ⟩ [array destructuring](https://javascript.info/destructuring-assignment#array-destructuring)
  {% endtab %}

{% tab title="👥 相關" %}

* [nested](https://lochiwei.gitbook.io/web/js/grammar/op/assign/destruct/nested "mention")
  {% endtab %}

{% tab title="⬇️ 應用" %}

* [a1notationtorowcolumn](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation/a1notationtorowcolumn "mention") - <mark style="color:blue;">`"AA3"`</mark> to <mark style="color:blue;">`[ "AA", "3" ]`</mark>.
  {% endtab %}
  {% endtabs %}
