0

I want to create a function that from that objects creates one object with recursion.

For example:

var arr1 = { 
'house' : {
   'doors' : 'black',
   'windows' : 'white',
},
'car' : 'audi'
};

var arr2 = { 
'house' : {
   'doors' : 'yellow',
   'windows' : 'red',
},
'car' : 'bmw',
'wife' : 'hot'
};

and as result I want to get:

arr3 = { 
'house' : {
   'doors' : 'black yellow',
   'windows' : 'white red',
},
'car' : 'audi bmw',
'wife' : 'hot'
};

I've got a function that combines two objects but not if some values are object how can I do that?

function mergeOptions(obj1,obj2){
    var obj3 = {};
    for(var attrname in obj1)
    {
            obj3[attrname] = obj1[attrname];
    }
    for(var attrname in obj2)
    {
        if ( obj3[attrname] != 'undefined')
            obj3[attrname] += ' ' + obj2[attrname];
        else
            obj3[attrname] = obj2[attrname];
        alert( obj1[attrname]);
    }
    return obj3;
}

Also in this function is a but that it in second loop adds undefined to an existing string. No idea why.

Any ideas how to create such a function?

Cirvis
  • 382
  • 1
  • 4
  • 15
  • 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) – Jeremy J Starcher Aug 15 '14 at 15:28
  • 3
    `if (typeof foo === 'object') { /* do recursive call */ }` See also http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json and http://stackoverflow.com/q/13861312/218196 – Felix Kling Aug 15 '14 at 15:30
  • I've checked that thread, there is no sign of recursion. – Cirvis Aug 15 '14 at 15:30
  • 1
    Instead of merging to: `'house' : { 'doors' : 'black yellow', 'windows' : 'white red', }'` You might consider arrays instead of strings: `'house' : { 'doors' : ['black', 'yellow'], 'windows' :['white', 'red'], }` – Jonathan M Aug 15 '14 at 15:30
  • @Cirvis, the second answer on Jeremy's thread is about [`jQuery.extend`](http://api.jquery.com/jQuery.extend/) which has a recursive option. You can use it like this `obj3 = $.extend(true, {}, obj1, obj2)` – parchment Aug 15 '14 at 15:39
  • Yes, thank you. Made it happen. Should I post result? – Cirvis Aug 15 '14 at 15:43

1 Answers1

1

Things are easier if you start coding the recursive function from your base case (non objects)

// receives two things (not only objects)
// returns a merged version of them
function merge(x, y){
    var tx = typeof x;
    var ty = typeof y;

    if(tx !== ty){ /*throw an error*/ }    

    if(tx === 'string'){
        return tx + ' ' + ty;
    }else if (tx === 'number'){
        //???
    }else if (tx === 'object'){

        var out = {};
        for(var k in x){
            if(!y.hasOwnProperty(k)){
                out[k] = x[k];
            }
        }
        for(var k in y){
            if(!x.hasOwnProperty(k)){
                out[k] = y[k];
            }
        }
        for(var k in x){
            if(y.hasOwnProperty(k)){
                out[k] = merge(x[k], y[k]);
            }
        }
        return out;

    }
}
hugomg
  • 68,213
  • 24
  • 160
  • 246