This is my code :
var a=[1,2,3] b=$.clone(a) alert(b)
Doesn't jQuery have a 'clone' method? How can I clone an array using jQuery?
Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array. To create a real copy of an array, you need to copy over the value of the array under a new value variable.
To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.
Just use Array.prototype.slice
.
a = [1]; b = a.slice();
JSFiddle - http://jsfiddle.net/neoswf/ebuk5/
What about the jQuery.merge ?
copy = $.merge([], a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With