Skip to content

Instantly share code, notes, and snippets.

@vishwasjois
Forked from penguinboy/Object Flatten
Created May 10, 2016 02:00
Show Gist options
  • Save vishwasjois/5d346ef054eb2f40feb571b161e0b2ae to your computer and use it in GitHub Desktop.
Save vishwasjois/5d346ef054eb2f40feb571b161e0b2ae 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