I created this method based on This question, I modified it to be recursive. It seems to work, but the issue I am having is that I am getting an error that says:
Uncaught TypeError: this.config.routes.forEach is not a function
var obj = {
config: {
maxLoadLoop: 5,
author: {color: "red"}
},
run: function(settings){
this.config = this.mergeOptions(this.config, settings);
this.config.routes.forEach(function(){/* do some stuff */});
},
mergeOptions: function(obj1, obj2){
var obj3 = {};
for(var attrname in obj1){
obj3[attrname] = obj1[attrname];
}
for(var attrname in obj2){
if(Array.isArray(obj2[attrname]) || typeof obj2[attrname] === "object"){
obj3[attrname] = this.mergeOptions(obj3[attrname], obj2[attrname]);
}else{
obj3[attrname] = obj2[attrname];
}
}
return obj3;
}
};
obj.run(myCustomSettings);
I am merging the following two objects:
{
maxLoadLoop: 5,
author: {color: "red"}
}
and this (json converted to object):
{
"author": {
"name": "Me",
"email": "my email"
},
"routes": [
{
"route": "/home",
"template": "/templates/home.html",
"default": true
},
{
"route": "/games",
"template": "/templates/games.html"
}
]
}
The two seem to merge fine, except that I get the error mentioned above...