This content originally appeared on CodeSource.io and was authored by Deven
If you want to clone an Array in Javascript we can use the spread operator to expand our array into items and clone it into a new array:
consider the following example:
const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = [...numbers];
Another better approach is using Array.slice()
method with no arguments. consider the following example.
const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = numbers.slice();
The post JavaScript Clone an Array example appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-02-08T14:20:01+00:00) JavaScript Clone an Array example. Retrieved from https://www.scien.cx/2021/02/08/javascript-clone-an-array-example/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.