Skip to content

Instantly share code, notes, and snippets.

@danielbreves
Last active January 1, 2016 02:28
Show Gist options
  • Save danielbreves/8079008 to your computer and use it in GitHub Desktop.
Save danielbreves/8079008 to your computer and use it in GitHub Desktop.
A function to flatten objects or arrays. Depends on underscore.js.
function flattenObject(obj, initial) {
var flatObject = initial || {};
Object.keys(obj).forEach(function(key) {
result = _.result(obj, key);
if (_.isArray(result)) { result = flattenObject(result, []); }
else if (_.isObject(result)) { result = flattenObject(result); }
flatObject[key] = result;
});
return flatObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment