Created
January 14, 2016 23:00
-
-
Save bmuller/a117669612f917a15a1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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