0
copying arrays? (js)
so lets say we have an array named 'arr'. var arr = ["test"] now when i make another copy of it, it simply "links" the array: var arr2 = arr arr2.push("test2") console.log(arr) //["test", "test2"] how can i make an actual copy of an array instead of linking them like this?
2 odpowiedzi
0
There are some ways to copy an array.
*Use slice
var arr1 = [0, 1, 2, 3, 4];
var arr2 = arr1.slice(0, arr1.length);
(Instead, you can use arr1.slice())
*Use concat
var arr1 = [0, 1, 2, 3, 4];
var arr2 = arr1.concat();
0
var arr2 = arr.slice(0);