0

I am trying to join all of the data objects into one where they are all chained

var person1 = {
    hello: {
        "david": {
            "hours": "44",
            "money": "22"
        }
    }
};


var person2 = {
    world: {
        "pearce": {
            "hours": "21",
            "money": "11"
        }
    }
};

I want it to look like this:

var mergedObjects = {
    hello: {
        "david": {
            "hours": "44",
            "money": "22"
        }
    },
    world: {
        "pearce": {
            "hours": "21",
            "money": "11"
        }
    }
};

I know how to do this with Arrays, but how can i do this in javascript as a JSON object maintaining the correct structure

Rafa Tost
  • 389
  • 5
  • 13
  • 1
    Did you mean to have data appear twice in your output? That won't be a valid JS object. – syazdani Nov 13 '13 at 05:30
  • 1
    you can't do that... key in an object will be unique.. I'll advice using an array to store this structure – Arun P Johny Nov 13 '13 at 05:30
  • It's worth noting that a Javascript Array is perfectly valid JSON as well, in the event that you're just looking to find something that works as JSON. – Jeff Sisson Nov 13 '13 at 05:33
  • sorry have updated, that was a bad example, was trying to merge children of the two objects into one – Rafa Tost Nov 13 '13 at 05:35
  • 1
    possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – Chris Baker Nov 13 '13 at 05:46
  • Did you mean to tag jquery? You could use `$.extend(merged,person1,person2);` – fbynite Nov 13 '13 at 05:46

3 Answers3

1

Check this out....

Answer is present here...How can I merge properties of two JavaScript objects dynamically?

JS Fiddle

JS:

 function mergeRecursive(obj1, obj2) {

  for (var p in obj2) {
    try {
      // Property in destination object set; update its value.
      if ( obj2[p].constructor==Object ) {
        obj1[p] = mergeRecursive(obj1[p], obj2[p]);

      } else {
        obj1[p] = obj2[p];

      }

    } catch(e) {
      // Property in destination object not set; create it and set its value.
      obj1[p] = obj2[p];

    }
  }

  return obj1;
}

var person1 = {
    hello: {
        "david": {
            "hours": "44",
            "money": "22"
        }
    }
};


var person2 = {
    world: {
        "pearce": {
            "hours": "21",
            "money": "11"
        }
    }
};

console.log(mergeRecursive(person1, person2));
Community
  • 1
  • 1
Nikhil N
  • 4,507
  • 1
  • 35
  • 53
  • This code is taken from this answer, verbatim: http://stackoverflow.com/a/383245/610573 -- please give credit when using code you didn't write. – Chris Baker Nov 13 '13 at 05:45
0

check this post : link

var person1 = {
    hello: {
        "david": {
            "hours": "44",
            "money": "22"
        }
    }
};


var person2 = {
    world: {
        "pearce": {
            "hours": "21",
            "money": "11"
        }
    }
};


function merge_options(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;
}



merge_options(person1,person2);
Community
  • 1
  • 1
Ankit Tyagi
  • 2,381
  • 10
  • 19
  • 1
    This code is taken directly from this answer: http://stackoverflow.com/a/171256/610573 -- please give credit when using code you didn't write. – Chris Baker Nov 13 '13 at 05:45
0

You can easily do this with jQuery:

var person1 = {
    hello: {
        "david": {
            "hours": "44",
            "money": "22"
         }
    }
};


var person2 = {
    world: {
        "pearce": {
            "hours": "21",
            "money": "11"
        }
    }
};

var mergedObjects = $.extend({}, person1, person2);

console.dir(mergedObjects);
moritzpflaum
  • 729
  • 4
  • 10