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?