You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/JavaScript_Advance/destructuring.md
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,4 +32,46 @@ This is a bit repetative and now with ES6 destructuring assignment, completely u
32
32
const { name, age, college } = a;
33
33
```
34
34
35
-
This may look a bit weird, but let's walk through it together. You can see that the open bracket directly follows the `const`, followed by the three different properties that we are assigning to variables by the same name. In essence what we are doing is extracting the value of `name`, `age`, and `college` from the `a` object and assigning those values to `const` variables by the exact same name.
35
+
This may look a bit weird, but let's walk through it together. You can see that the open bracket directly follows the `const`, followed by the three different properties that we are assigning to variables by the same name. In essence what we are doing is extracting the value of `name`, `age`, and `college` from the `a` object and assigning those values to `const` variables by the exact same name.
36
+
37
+
## Destructuring and Assignment
38
+
There is a time when you want to destruct an item from an array and immediately name it, with javascript you can do it.
39
+
40
+
```js
41
+
const { name:surname } = a;
42
+
```
43
+
If you console the name and surname both will display print `Swapnil`.
44
+
45
+
### Destructuring Array
46
+
Sometime data is saved in an array datastructure, we can destruct them if we want
Always check your array length before, and then use the power of destructuring.
67
+
68
+
## Destructuring and Combination
69
+
We can use our destructuring skills to get a specific item and combine the rest data. How to do it? Basically we need to use javascript `Spread operator`
70
+
71
+
```js
72
+
constbigArray= [ 1,2,3,4,5,6,6,7,8,9];
73
+
```
74
+
```js
75
+
const [firstItem, ...remainItems] = bigArray
76
+
```
77
+
The output will be first item will be `1` and remaining items will be combained in array `[2, 3, 4, 5, 6, 6, 7, 8, 9]`
0 commit comments