Created
June 12, 2019 19:01
-
-
Save atomize/44de044c40eae9b9b715b469c5110138 to your computer and use it in GitHub Desktop.
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 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