Last active
February 14, 2021 14:02
-
-
Save Zazza/5287170b9ceaa9cb459b303350b29a5d to your computer and use it in GitHub Desktop.
ParseJson with anonymous func + yield
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 parseObject(obj) { | |
let result = [] | |
let pathesArray = function* inObject(obj, path) { | |
for(let item in obj) { | |
let currentPath = path + '.' + item | |
if (path === '') | |
currentPath = item | |
if (!Array.isArray(obj[item])) { | |
yield * inObject(obj[item], currentPath) | |
} else { | |
yield currentPath | |
} | |
} | |
} | |
for(let value of pathesArray(JSON.parse(obj), '')) { | |
result[result.length] = value | |
} | |
return result | |
} | |
const input = '{"bookkeeping":{"contacts": {"pones":[123456789,234567890,345678901],"emails":["[email protected]"]},"staff": {"total":2,"employees":{"full_time":[{"name":"Sonoo","email":"[email protected]","salary":56000,"married":true},{"name":"Ram","email":"[email protected]","salary":65000,"married":true},{"name":"Bob","email":"[email protected]","salary":42000,"married":true}]}}}}' | |
let result = parseObject(input) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment