Javascript合并两个对象

时间:2015-08-11 19:20:17

标签: javascript object merge

我正在寻找像PHP var modelViewMatrix: Matrix4x4 这样的javascript function。 根据我的阅读,javascript中没有这样的东西,我想知道如何实现类似的东西。

问题:

我有两个array_merge_recursive()

objects

在那些 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] } }; 中,我需要创建一个像这样的新人:

objects

我在做什么:

   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]
      }
    }

我正在使用nodejs,我尝试过一些没有成功的库, 我也在这里阅读了一些类似的问题,但没有找到解决我问题的方法。关于我如何解决的任何建议? 谢谢大家的时间。

时间解决方案

暂时用/* 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 < 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);

解决了我的问题
array.push()

2 个答案:

答案 0 :(得分:0)

此函数假定对象x中的所有键和子键都在对象y中,反之亦然。

function merge(x, y) {
    var result = {};
    Object.keys(x).forEach(
        function (k) {
            result[k] = {};
            Object.keys(x[k]).forEach(
                function (j) {
                    if (j in y[k]) {
                        result[k][j] = x[k][j].concat(y[k][j]);
                    }
                });
        });
    return result;    
}

试运行:

merge(obj1, obj2);
=> { AUDCAD: 
   { t: [ 1439238600, 1439238600, 111 ],
     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 ] } }

答案 1 :(得分:-1)

Object.prototype.merge = function(obj) {

    if (typeof this !== 'object' 
       || obj === undefined 
       || typeof obj !== 'object')
        return;

    var self = this,
       arr = {},
       i;

    for (i in self) {
        if (typeof obj[i] !== 'undefined'
         && typeof self[i] === typeof obj[i]) {
            self[i].merge(obj[i]);
            arr[i] = true;
         }
    }

    for (i in obj) {
        if (!arr[i])
           self[i] = obj[i];
    }
};

Array.prototype.merge = function(obj) {
    if (typeof obj !== 'undefined'
        && !(obj instanceof Array))
        return;

    var self = this,
       i;
    for (i = 0; i < obj.length; i++)
       self.push(obj[i]);
};

运行

obj1.merge(obj2);

数据将附加到obj1