title: Javascript merge two objects
body: <p>I am looking for a javascript <code>function</code> like PHP <code>array_merge_recursive()</code>.
  From what I've read, there is no such thing in javascript, and I want to know how I can implement something similar.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>I have two <code>objects</code>:</p>
  
  <pre><code> var obj1 = {
    "AUDCAD": {
      "t": [1439238600, 1439238600],
      "o": [0.9646, 0.9646],
      "h": [0.9646, 0.9646],
      "l": [0.9645, 0.9645],
      "c": [0.9645, 0.9645],
      "v": [15, 15]
    },
    "AUDCHF": {
      "t": [1439238600, 1439238600],
      "o": [0.7291, 0.7291],
      "h": [0.7292, 0.7292],
      "l": [0.729, 0.729],
      "c": [0.7291, 0.7291],
      "v": [6, 6]
    }
  };
  
  var obj2 = {
    "AUDCAD": {
      "t": [111],
      "o": [111],
      "h": [111],
      "l": [111],
      "c": [111],
      "v": [111]
    },
    "AUDCHF": {
      "t": [111],
      "o": [111],
      "h": [111],
      "l": [111],
      "c": [111],
      "v": [111]
    }
  };
  </code></pre>
  
  <p>Of those <code>objects</code> I need to create a new one like this:</p>
  
  <pre><code>   var obj3 =  {
        "AUDCAD": {
          "t": [1439238600, 1439238600, 111], //Note that the value "111" is added from obj2 to the end of obj1.
          "o": [0.9646, 0.9646, 111],
          "h": [0.9646, 0.9646, 111],
          "l": [0.9645, 0.9645, 111],
          "c": [0.9645, 0.9645, 111],
          "v": [15, 15, 111]
        },
        "AUDCHF": {
          "t": [1439238600, 1439238600, 111],
          "o": [0.7291, 0.7291, 111],
          "h": [0.7292, 0.7292, 111],
          "l": [0.729, 0.729, 111],
          "c": [0.7291, 0.7291, 111],
          "v": [6, 6, 111]
        }
      }
  </code></pre>
  
  <p><strong>What im doing:</strong></p>
  
  <pre><code>/* 
  Code seen here: 
  http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically
  Author: Scdev
  */
  
  function extendObjects() {
  
    var newObject = {};
    var overwriteValues = false;
    var overwriteObjects = false;
  
    for (var indexArgument = 0; indexArgument &lt; arguments.length; indexArgument++) {
  
      if (typeof arguments[indexArgument] !== 'object') {
  
        if (arguments[indexArgument] == 'overwriteValues_True') {
  
          overwriteValues = true;
        } else if (arguments[indexArgument] == 'overwriteValues_False') {
  
          overwriteValues = false;
        } else if (arguments[indexArgument] == 'overwriteObjects_True') {
  
          overwriteObjects = true;
        } else if (arguments[indexArgument] == 'overwriteObjects_False') {
  
          overwriteObjects = false;
        }
  
      } else {
  
        extendObject(arguments[indexArgument], newObject, overwriteValues, overwriteObjects);
      }
  
    }
  
    function extendObject(object, extendedObject, overwriteValues, overwriteObjects) {
  
      for (var indexObject in object) {
  
        if (typeof object[indexObject] === 'object') {
  
          if (typeof extendedObject[indexObject] === "undefined" || overwriteObjects) {
            extendedObject[indexObject] = object[indexObject];
          }
  
          extendObject(object[indexObject], extendedObject[indexObject], overwriteValues, overwriteObjects);
  
        } else {
  
          if (typeof extendedObject[indexObject] === "undefined" || overwriteValues) {
            extendedObject[indexObject] = object[indexObject];
          }
  
        }
  
      }
  
      return extendedObject;
  
    }
  
    return newObject;
  }
  
  
  var newExtendedObject = extendObjects('overwriteValues_False', 'overwriteObjects_False', obj1, obj2);
  </code></pre>
  
  <p>I am using nodejs and I have tried without success with some libraries,
  I've also read some similar questions here but not found a solution to my problem. Any suggestions on how I can solve?
  Thanks everyone for your time.</p>
  
  <p><strong>Temporal solution</strong></p>
  
  <p>Temporarily solved my problem with <code>array.push()</code></p>
  
  <pre><code>//Pseudo-code    
  obj1[0]["AUDCAD"]["t"].push(obj2[0]["AUDCAD"]["t"]);
  </code></pre>