October 2018
Intermediate to advanced
590 pages
15h 5m
English
You can use the same syntax of the rest parameters in the destructuring assignment to put the remainder of the elements of an array into another array. Here is an example:
let [first, ...others] = ['Traveling', 'Swimming', 'Shopping'];console.log(others); // ["Swimming", "Shopping"]
As you can see, the second and third items of the array have been copied into the others variable. We can use this syntax to copy an array. However, this is only a shallow clone. When the elements of the array are objects, changes to an object's property of the copied array will be seen in the original array because essentially, the elements of both arrays reference the same object. Here is an example:
1. const fruits = [{name:'Apple', price:100},{name:'Orange', ...Read now
Unlock full access