Possible Duplicate:
What is the most efficient way to clone a JavaScript object?
I need to copy an (ordered, not associative) array of objects. I’m using jQuery. I initially tried
jquery.extend({}, myArray)
but, naturally, this gives me back an object, where I need an array (really love jquery.extend, by the way).
So, what’s the best way to copy an array?
Since Array.slice() does not do deep copying, it is not suitable for multidimensional arrays:
Note that although I’ve used
shift().shift()above, the point is just thatb[0][0]contains a pointer toa[0][0]rather than a value.Likewise
delete(b[0][0])also causesa[0][0]to be deleted andb[0][0]=99also changes the value ofa[0][0]to 99.jQuery’s
extendmethod does perform a deep copy when a true value is passed as the initial argument: