Is there a method to clone an array in jQuery?

JavascriptJqueryArraysClone

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

Just use Array.prototype.slice.

a = [1];
b = a.slice();

JSFiddle - http://jsfiddle.net/neoswf/ebuk5/

Solution 2 - Javascript

What about the jQuery.merge ?

copy = $.merge([], a);

Solution 3 - Javascript

Change

b=$.clone(a) to b=$(this).clone(a) but it some time dont work

but is reported

http://www.fusioncube.net/index.php/jquery-clone-bug-in-internet-explorer

Solution you use simple inbuilt clone function of javascript

var a=[1,2,3];
b=clone(a);
alert(b);

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;
    var temp = obj.constructor();
    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
}

-ConroyP

A great alternative is

 // Shallow copy
  var b = jQuery.extend({}, a);

  // Deep copy
  var b = jQuery.extend(true, {}, a);

-John Resig

Check similar post

Solution 4 - Javascript

This is how i've done it :

var newArray = JSON.parse(JSON.stringify(orgArray));

this will create a new deep copy not related to the first one (not a shallow copy).

also this obviously will not clone events and functions, but the good thing you can do it in one line and it can be used for any king of object (arrays, strings, numbers, objects ...)

Solution 5 - Javascript

ES6 Please use spread

let arrayCopy = [...myArray];

Solution 6 - Javascript

try

if (!Array.prototype.clone) {
    Array.prototype.clone = function () {
        var arr1 = new Array();
        for (var property in this) {
            arr1[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]
        }
        return arr1;
    }​
}

use as

var a = [1, 2, 3]
b = a;
a.push(4)
alert(b); // alerts [1,2,3,4]
//---------------///
var a = [1, 2, 3]
b = a.clone();
a.push(4)
alert(b); // alerts [1,2,3]

Solution 7 - Javascript

Another option is to use Array.concat:

var a=[1,2,3]
var b=[].concat(a);

Solution 8 - Javascript

var a=[1,2,3]
b=JSON.parse(JSON.stringify(a));
document.getElementById("demo").innerHTML = b;

<p id="demo"></p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzjm1126View Question on Stackoverflow
Solution 1 - Javascriptmeder omuralievView Answer on Stackoverflow
Solution 2 - JavascriptastgtcivView Answer on Stackoverflow
Solution 3 - JavascriptPramendra GuptaView Answer on Stackoverflow
Solution 4 - JavascriptChtioui MalekView Answer on Stackoverflow
Solution 5 - JavascriptzloctbView Answer on Stackoverflow
Solution 6 - JavascriptReigelView Answer on Stackoverflow
Solution 7 - JavascriptMike RatcliffeView Answer on Stackoverflow
Solution 8 - JavascriptPradeep KumarView Answer on Stackoverflow