Skip to content

Instantly share code, notes, and snippets.

@atomize
Created June 12, 2019 19:01
Show Gist options
  • Save atomize/44de044c40eae9b9b715b469c5110138 to your computer and use it in GitHub Desktop.
Save atomize/44de044c40eae9b9b715b469c5110138 to your computer and use it in GitHub Desktop.
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;
}
var obj = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 },
path = getFlatObject(json1);
console.log(path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment