Skip to content

Instantly share code, notes, and snippets.

@m1ggy
Created June 17, 2022 13:13
Show Gist options
  • Select an option

  • Save m1ggy/7b6ecd7c89aae350724e2479abd7caa0 to your computer and use it in GitHub Desktop.

Select an option

Save m1ggy/7b6ecd7c89aae350724e2479abd7caa0 to your computer and use it in GitHub Desktop.
//@source link [https://stackoverflow.com/questions/44134212/best-way-to-flatten-js-object-keys-and-values-to-a-single-depth-array]
function flattenObject(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object' && ob[i] !== null) {
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