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?

6
  • possible duplicate of How can I merge properties of two JavaScript objects dynamically? Commented Aug 15, 2014 at 15:28
  • 3
    if (typeof foo === 'object') { /* do recursive call */ } See also stackoverflow.com/questions/11922383/… and stackoverflow.com/q/13861312/218196 Commented Aug 15, 2014 at 15:30
  • I've checked that thread, there is no sign of recursion. Commented Aug 15, 2014 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'], } Commented Aug 15, 2014 at 15:30
  • @Cirvis, the second answer on Jeremy's thread is about jQuery.extend which has a recursive option. You can use it like this obj3 = $.extend(true, {}, obj1, obj2) Commented Aug 15, 2014 at 15:39

1 Answer 1

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;

    }
}
Sign up to request clarification or add additional context in comments.

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.