Skip to content

Instantly share code, notes, and snippets.

@ziishaned
Created June 26, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziishaned/d7083a8e2ad3fdb16c4e8d81dd422679 to your computer and use it in GitHub Desktop.
Save ziishaned/d7083a8e2ad3fdb16c4e8d81dd422679 to your computer and use it in GitHub Desktop.
Get flat object
function getFlatObject(object) {
function iter(o, p) {
if (Array.isArray(o) ){
o.forEach(function (a, i) {
iter(a, p.concat(i));
});
return;
}
if (o !== null && typeof o === 'object') {
Object.keys(o).forEach(function (k) {
iter(o[k], p.concat(k));
});
return;
}
path[p.join('.')] = o;
}
var path = {};
iter(object, []);
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment