I have an object called defaultOptions
and a function that accepts as an argument an object called newValues
meant to override the values in defaultOptions
with its own. The values that are not specified in newValues
should remain default.
var configOptions = {
isActive: false,
name: "",
description: "",
category: "",
group: "default"
}
publishOptions: function(newValues){
$.each(newValues, function(key, value){
if(configOptions.hasOwnProperty(key)){
configOptions[key] = value;
}
else{
configOptions[key] = value;
}
});
console.log(configOptions);
}
For example, if I use this method like so:
var myOptions = {
name: "option one",
category: "the best"
}
publishOptions(myOptions)
I would expect configOptions
to look like this:
configOptions = {
isActive: false,
name: "option one",
description: "",
category: "the best",
group: "default"
}
I'm using jQuery's each
method to map the values from newValues
to configOptions
, but I suspect there is a much more succinct and elegant way to do this. I would appreciate any suggestions.