|
copy Nested Structures / Objects in JavascriptHi I'm trying to copy a nested structure in the Javascript patch so i can manipulate it. The function i came up with does work, but it messes up the order of the structure. function cloneObject(oldObject) { var newObject = new Object() for (i in oldObject) { if (typeof oldObject[i] == 'object') { newObject[i] = new cloneObject(oldObject[i]); } else newObject[i] = oldObject[i]; } return newObject } Anyone has a suggestion? i'm kinda at the end of my javascript knowledge here. thanx
|
You should use an Array() rather than an Object(), if the order of items is important.
a|x
Thanx!
I ended up making a second function that sorts an Object into an Array:
These functions can be helpfull when using the File Tools (structure to/from file patch )