如何递归合并2个javascript对象?

时间:2015-09-25 23:53:34

标签: javascript jquery arrays

我有两个对象需要合并并保持所有属性的机智,尝试使用jQuery $ .extend但我无法让它工作。我尝试了所有关于如何合并javascript对象的帖子,但简直无法让它工作。

var thz_icon_source = {"Spinners":["spinnericon1","spinnericon2"],"Awesome":["awesomeicon1","awesomeicon2"]};
var fa_icon_source = {"Spinners":["faspinner1","faspinner2"],"Awesome":["faawesome1","faawesome2"]};
var new_source ={};
$.extend(new_source,fa_icon_source,thz_icon_source);

console.log(thz_icon_source);
console.log(fa_icon_source);
console.log(new_source);

所需的输出应该像

{
"Spinners":["faspinner1","faspinner2","spinnericon1","spinnericon2"],
"Awesome":["faawesome1","faawesome2","awesomeicon1","awesomeicon2"]
}

这篇文章Merge two json/javascript arrays in to one array有一个简单的对象,我的不一样。

5 个答案:

答案 0 :(得分:4)

Demo

function mergeJSON(json1,json2)
{
    var result = json1 ;
    for (var prop in json2) 
    {
        if (json2.hasOwnProperty(prop)) 
        {
            result[prop] = result[prop].concat(json2[prop]);
        }
    }
    return result;
}

答案 1 :(得分:1)

$.extend合并缺少的属性,它没有组合共同的属性。你需要写一个循环。



var thz_icon_source = {
  "Spinners": ["spinnericon1", "spinnericon2"],
  "Awesome": ["awesomeicon1", "awesomeicon2"]
};
var fa_icon_source = {
  "Spinners": ["faspinner1", "faspinner2"],
  "Awesome": ["faawesome1", "faawesome2"]
};
var new_source = {};
// First add in the new elements from thz_icon_source
$.extend(new_source, fa_icon_source, thz_icon_source);
// Now merge the common elements
$.each(fa_icon_source, function(k, e) {
  if (thz_icon_source.hasOwnProperty(k)) {
    new_source[k] = e.concat(thz_icon_source[k]);
  }
});

console.log(thz_icon_source);
console.log(fa_icon_source);
console.log(new_source);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以使用此原型以您希望的方式合并两个或多个对象:

Object.prototype.assignDeep = function() {
    var self = this;
    Object.keys(arguments).forEach(obj => {
        Object.keys(self).forEach(val => {
            if (arguments[obj].hasOwnProperty(val)) {
                var tmp = arguments[obj][val] instanceof Array ? arguments[obj][val] : [arguments[obj][val]];
                self[val] = self[val].concat(tmp);
            }  
        });
    });
    return self;
}
    
    
    
var thz_icon_source = {"Spinners":["spinnericon1","spinnericon2"],"Awesome":["awesomeicon1","awesomeicon2"]};
var fa_icon_source = {"Spinners":["faspinner1","faspinner2"],"Awesome":["faawesome1","faawesome2"]};
var b = thz_icon_source.assignDeep(fa_icon_source);
console.log(b);

答案 3 :(得分:0)

您应该使用.concat()的循环:

function objectConcatArrays(){
  var a = arguments, o = {};
  for(var i=0,l=a.length; i<l; i++){
    for(var p in a[i]){
      if(p in o){
        o[p] = o[p].concat(a[i][p]);
      }
      else{
        o[p] = a[i][p];
      }
    }
  }
  return o;
}
var thz_icon_source = {"Spinners":["spinnericon1","spinnericon2"],"Awesome":["awesomeicon1","awesomeicon2"]};
var fa_icon_source = {"Spinners":["faspinner1","faspinner2"],"Awesome":["faawesome1","faawesome2"]};
var res = objectConcatArrays(thz_icon_source, fa_icon_source);
console.log(res);

每个参数代表一个数组对象。如果你愿意,可以添加更多。

答案 4 :(得分:-1)

两个对象都进入数组?

var thz_icon_source = {"Spinners":["spinnericon1","spinnericon2"],"Awesome":["awesomeicon1","awesomeicon2"]};
var fa_icon_source = {"Spinners":["faspinner1","faspinner2"],"Awesome":["faawesome1","faawesome2"]};
var new_source = new Array();
new_source[0] = thz_icon_source;
new_source[1] = fa_icon_source ;

console.log(new_source);