Deep Cloning an Object | Newbie | | Join Date: Sep 2007
Posts: 17
| | |
How does one perform a deep clone (by value, not by reference) to an object in JavaScript?
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
hi ...
you may use something like the following: -
var a = {
-
foo : { test: 1, test1: 2 },
-
bar : function(a) { alert(a) },
-
foobar: 'foobar1'
-
};
-
-
function clone_obj(obj) {
-
var c = {};
-
-
for (var i in obj) {
-
var prop = obj[i];
-
-
if (typeof prop == 'object') {
-
c[i] = clone_obj(prop);
-
} else {
-
c[i] = prop;
-
}
-
}
-
-
return c;
-
}
-
-
var b = clone_obj(a);
-
kind regards
ps: if you encounter problems with that let me know :)
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
hmm ... we have to improve it for properties that are arrays ... :)
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
improved version: -
var a = {
-
foo : { test: 1, test1: 2 },
-
bar : function(a) { alert(a) },
-
foobar: 'foobar1',
-
arr : [1, 2, 3, 4]
-
};
-
-
function clone_obj(obj) {
-
var c = {};
-
-
for (var i in obj) {
-
var prop = obj[i];
-
-
if (typeof prop == 'object') {
-
if (prop instanceof Array) {
-
c[i] = [];
-
-
for (var j = 0; j < prop.length; j++) {
-
c[i].push(prop[j]);
-
}
-
} else {
-
c[i] = clone_obj(prop);
-
}
-
} else {
-
c[i] = prop;
-
}
-
}
-
-
return c;
-
}
-
-
var b = clone_obj(a);
-
kind regards
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
arrrgh ... next testcase to check for :)
now the function is to be improved for the case that objects are contained i an array: -
var a = {
-
foo : { test: 1, test1: 2 },
-
bar : function(a) { alert(a) },
-
foobar: 'foobar1',
-
arr : [1, [8, 9], {1: 1, 2: 2}, 4]
-
};
-
-
function clone_obj(obj) {
-
var c = obj instanceof Array ? [] : {};
-
-
for (var i in obj) {
-
var prop = obj[i];
-
-
if (typeof prop == 'object') {
-
if (prop instanceof Array) {
-
c[i] = [];
-
-
for (var j = 0; j < prop.length; j++) {
-
if (typeof prop[j] != 'object') {
-
c[i].push(prop[j]);
-
} else {
-
c[i].push(clone_obj(prop[j]));
-
}
-
}
-
} else {
-
c[i] = clone_obj(prop);
-
}
-
} else {
-
c[i] = prop;
-
}
-
}
-
-
return c;
-
}
-
-
var b = clone_obj(a);
-
kind regards
| | Newbie | | Join Date: Sep 2007
Posts: 17
| | | re: Deep Cloning an Object Quote:
Originally Posted by gits arrrgh ... next testcase to check for :)
now the function is to be improved for the case that objects are contained i an array: -
var a = {
-
foo : { test: 1, test1: 2 },
-
bar : function(a) { alert(a) },
-
foobar: 'foobar1',
-
arr : [1, [8, 9], {1: 1, 2: 2}, 4]
-
};
-
-
function clone_obj(obj) {
-
var c = obj instanceof Array ? [] : {};
-
-
for (var i in obj) {
-
var prop = obj[i];
-
-
if (typeof prop == 'object') {
-
if (prop instanceof Array) {
-
c[i] = [];
-
-
for (var j = 0; j < prop.length; j++) {
-
if (typeof prop[j] != 'object') {
-
c[i].push(prop[j]);
-
} else {
-
c[i].push(clone_obj(prop[j]));
-
}
-
}
-
} else {
-
c[i] = clone_obj(prop);
-
}
-
} else {
-
c[i] = prop;
-
}
-
}
-
-
return c;
-
}
-
-
var b = clone_obj(a);
-
kind regards
Thanks, but this code gives me an error:
Error: too much recursion
When I try to clone a <td> object...
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
you cannot clone dom-objects with that ... for that you can use regular dom methods ... cloneNode();
kind regards
| | Newbie | | Join Date: Sep 2007
Posts: 17
| | | re: Deep Cloning an Object Quote:
Originally Posted by gits you cannot clone dom-objects with that ... for that you can use regular dom methods ... cloneNode();
kind regards This one works.
Thanks :)
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,246
| | | re: Deep Cloning an Object
hi ...
glad you got it working ... post back to the forum anytime you have more questions ...
kind regards
|  | Similar JavaScript / Ajax / DHTML bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|