1

I have 2 objects that I need to merge and keep all properties in tact, tried with jQuery $.extend but I cant get it to work . I tried all posts with how to merge javascript objects but simply cant get this to work.

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);

desired output should be like

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

This post Merge two json/javascript arrays in to one array has a simple object mine is not same as that one.

5
  • $.extend can't do that as it will overwrite properties it finds if $.extend is passed with the deep parameter set to true. If used the way you have used it, it will ignore properties if they already exist in the target object. Commented Sep 25, 2015 at 23:57
  • @papiro yes noticed that extend and merge cant help me there , seems like I need to loop and push in new one Commented Sep 25, 2015 at 23:58
  • possible duplicate of Merge two json/javascript arrays in to one array Commented Sep 26, 2015 at 0:04
  • @aug not it is not , I tried that one to , it is using concat which cant help me with my objects Commented Sep 26, 2015 at 0:06
  • @aug how about you try the code before you mark as duplicate. it is not the same object structure. Commented Sep 26, 2015 at 0:10

4 Answers 4

5

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Right on the money! nailed it!
1

$.extend merges in missing properties, it doesn't combine the properties that are in common. You need to write a loop.

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>

Comments

1

You can use this prototype to merge 2 or more objects the way you want it:

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);

Comments

0

You should use a loops with .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);

Each argument represents an Object of Arrays. Add more if you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.