destructuring an array in ES6 rest (#19424)
* destructuring an array in ES6 rest * fix: added const on line 56
This commit is contained in:
committed by
Randell Dawson
parent
44641ab13a
commit
c7ce87090f
@ -53,7 +53,7 @@ console.log(firstName, lastName); // undefined undefined
|
||||
You can still achieve the desired result using the following syntax.
|
||||
```js
|
||||
const obj = {propertyName: value}
|
||||
{propertyName: desiredVariableName} = obj
|
||||
const {propertyName: desiredVariableName} = obj
|
||||
```
|
||||
|
||||
Our previous example would be rewritten as follows:
|
||||
@ -63,3 +63,10 @@ const {first: firstName, last: lastName} = fullName;
|
||||
|
||||
console.log(firstName, lastName); // John Smith
|
||||
```
|
||||
**Array Destructuring with rest**
|
||||
When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest pattern:
|
||||
```js
|
||||
const [a, ...b] = [1, 2, 3];
|
||||
console.log(a); // 1
|
||||
console.log(b); // [2, 3]
|
||||
```
|
||||
|
Reference in New Issue
Block a user