Update index.md (#24596)
Fix typo in Spread Operator document, and an example of copy an array was added.
This commit is contained in:
committed by
Randell Dawson
parent
9f4739ed9d
commit
db1d633fe5
@ -5,7 +5,7 @@ title: Spread Operator
|
|||||||
|
|
||||||
The spread operator (`...`), allows to get the elements of a collection.
|
The spread operator (`...`), allows to get the elements of a collection.
|
||||||
|
|
||||||
One of the most commom uses is for `Node` Objects, when using query selectors in the browser, in order to make them iterable Array Objects:
|
One of the most common uses is for `Node` Objects, when using query selectors in the browser, in order to make them iterable Array Objects:
|
||||||
```javascript
|
```javascript
|
||||||
const paragraphs = document.querySelectorAll('p.paragraph');
|
const paragraphs = document.querySelectorAll('p.paragraph');
|
||||||
const arr = [...paragraphs];
|
const arr = [...paragraphs];
|
||||||
@ -19,6 +19,15 @@ const arr_final = [...arr_1, ...arr_2]
|
|||||||
// arr_final = [1, 2, 3, 4, 5, 6, 7, 8]
|
// arr_final = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Spread operator can be used to copy an Array.
|
||||||
|
```javascript
|
||||||
|
const arr_3 = [1, 2, 3, 4]
|
||||||
|
const reference = arr_3
|
||||||
|
const copy = [...arr_3]
|
||||||
|
copy.push(5)
|
||||||
|
// arr_3 = [1, 2, 3, 4]
|
||||||
|
// copy = [1, 2, 3, 4, 5]
|
||||||
|
|
||||||
The spread operator can also copy enumerable properties from one or more objects onto a new object:
|
The spread operator can also copy enumerable properties from one or more objects onto a new object:
|
||||||
```javascript
|
```javascript
|
||||||
const obj1 = {
|
const obj1 = {
|
||||||
@ -36,6 +45,7 @@ const obj2 = {
|
|||||||
const newObj = {...obj1, ...obj2);
|
const newObj = {...obj1, ...obj2);
|
||||||
|
|
||||||
// newObj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
|
// newObj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
|
||||||
|
|
||||||
```
|
```
|
||||||
### More Information:
|
### More Information:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user