Array Duplication Made Easy
by ilyas usal
To make a duplicate of an array proves more difficult than it seems. For
instance, if you try this piece of code:
It
will return:
a=1,2
b=1,2
Even though you've applied the method Array.pop on a only. b is
a reference to a, an alias if you prefer, not a copy, and modifying a
will modify b.
So here's the little trick that solves the problem. All you have to do is use
the Array.splice method (Array.concat
or Array.join will work too), with no parameter
to keep the whole array.
Hope this helps. Thanks to senocular and jbradley who helped contribute to
this.
Hope this tutorial helped. If you have any
questions, please post them on the forums at
//www.kirupa.com/forum/
|