Use slice
: var fruits2 = fruits.slice();
should do it.
Your jsFiddle, modified
See also: MDN
**Edit. I was a bit lazy, let's correct my answer to make up for that.
For an Array
of just values slice
is perfect. For an Array
of objects or arrays or a mix of values/objects/arrays, the Array
and Object
elements of the Array
to clone need cloning too. Otherwise they will be references to the original arrays or objects (so: not copies) and a change of one [of these references of arrays or objects] will be reflected in all 'clones' containing a reference to it.
To clone an Array
of Arrays/Objects/mixed values Array.map
is your friend. There are several methods to think of:
- creating a new instance with old data
var fruits1 = fruits.map(function(v) {return new Fruit(v.name);});
- using JSON
var fruits2 = fruits.map(function(v) {return JSON.parse(JSON.stringify(v));});
- create and use some cloning method
var fruits3 = fruits.map(function(v) {return cloneObj(v);});
In case 3, a method for cloning could look like:
function cloneObj(obj) {
function clone(o, curr) {
for (var l in o){
if (o[l] instanceof Object) {
curr[l] = cloneObj(o[l]);
} else {
curr[l] = o[l];
}
}
return curr;
}
return obj instanceof Array
? obj.slice().map( function (v) { return cloneObj(v); } )
: obj instanceof Object
? clone(obj, {})
: obj;
}
Using this cloneObj
method, Array.map
is obsolete.
You can also use var fruitsx = cloneObj(fruits);
The jsFiddle from the link above is modified to demonstrate these methods.
For Array.map
, see again MDN