Last active
January 1, 2016 02:28
-
-
Save danielbreves/8079008 to your computer and use it in GitHub Desktop.
A function to flatten objects or arrays. Depends on underscore.js.
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 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