copy

copy Nested Structures / Objects in Javascript

M.Oostrik's picture

Hi

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