Skip to content

Instantly share code, notes, and snippets.

@diegoarcega
Forked from penguinboy/Object Flatten
Created January 29, 2020 15:38
Show Gist options
  • Save diegoarcega/cf6830cef2494057b65c488a8deced88 to your computer and use it in GitHub Desktop.
Save diegoarcega/cf6830cef2494057b65c488a8deced88 to your computer and use it in GitHub Desktop.
Flatten javascript objects into a single-depth object
var flattenObject = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object') {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment