Skip to content

Instantly share code, notes, and snippets.

@sdizier
Created November 6, 2014 19:39
Show Gist options
  • Save sdizier/726a1b10123f45b03cd8 to your computer and use it in GitHub Desktop.
Save sdizier/726a1b10123f45b03cd8 to your computer and use it in GitHub Desktop.
Turn an object into an array of accessor strings
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