Created
November 6, 2014 19:39
-
-
Save sdizier/726a1b10123f45b03cd8 to your computer and use it in GitHub Desktop.
Turn an object into an array of accessor strings
This file contains 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 flatten(x, result, prefix) { | |
if(_.isObject(x) && !_.isArray(x)) { | |
_.each(x, function(v, k) { | |
result = flatten(v, result, prefix ? prefix + '.' + k : k) | |
}) | |
} else { | |
result[prefix] = x | |
} | |
return result | |
} | |
var obj = {a: {b: {c: [1,2]}, d: {z: [0,1], y: {l: {r:[0,1]}}}}}; | |
result = flatten(obj, {}, '') | |
keys = _.keys(result); | |
console.log(keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment