Skip to content

Instantly share code, notes, and snippets.

@bmuller
Created January 14, 2016 23:00
Show Gist options
  • Save bmuller/a117669612f917a15a1a to your computer and use it in GitHub Desktop.
Save bmuller/a117669612f917a15a1a to your computer and use it in GitHub Desktop.
function flatten(obj) {
var result = {};
if(obj === null || typeof obj !== 'object') {
// error
}
for(k in obj) {
value = obj[k];
if(value === null) {
// error. have to put this here because null is an object
} else if(typeof value === 'object') {
nvalue = flatten(value);
for(nk in nvalue)
result[k + '.' + nk] = nvalue[nk];
} else if(typeof value === 'string') {
result[k] = value;
} else if(typeof value === 'number') {
result[k] = value.toString();
} else {
// error
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment