It depends: is it always going to be an object like
var objToFlatten = {
"a": "b",
"c": "d",
"featuremap": {
"e": "f",
"g": "h"
}
}
Or could it potentially be multi-nested, with multiple objects to flatten? For example:
var objToFlatten = {
"a": "b",
"c": "d",
"featuremap": {
"e": "f",
"g": "h"
},
"someothermap": {
"e": "f",
"g": "h",
"nestedmap": {
"i": "j"
}
}
}
The first is easy-ish but hacky.
function copyFromObject(other) {
for (var propertyName in other) {
if (propertyName == 'featureMap') continue;
if (other.hasOwnProperty(propertyName)) {
this[propertyName] = other[propertyName];
}
}
return this;
}
var flattened = copyFromObject.call({}, objToFlatten);
The latter would be cleaner and would require a recursive solution. Also you'd need to figure out what you want to do about things like duplicated entries. What if you have two properties in two nested objects with the same name?