Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  174] [ 3]  / answers: 1 / hits: 36062  / 12 Years ago, thu, january 24, 2013, 12:00:00

I'm trying to create a copy of existing array and remove some items from array copy without impacting the original. I've tried this :



var new_arr = old_arr; //when I remove from new array the items from old array are also removed


How do I create entirely new copy of the existing array?



Update :



When I do this :



var new_arr = old_arr.slice();


then later :



new_arr[0].shift();
new_arr[1].shift();


The items from old_array get removed. This is a two dimensional array.


More From » javascript

 Answers
20

You can use two methods, this:



function clone (src) {
return JSON.parse(JSON.stringify(src));
}


or this:



var newArray = oldArray.slice();

[#80649] Wednesday, January 23, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradley

Total Points: 555
Total Questions: 102
Total Answers: 99

Location: Tajikistan
Member since Fri, Nov 27, 2020
4 Years ago
;