Skip to content

Instantly share code, notes, and snippets.

@Zazza
Last active February 14, 2021 14:02
Show Gist options
  • Save Zazza/5287170b9ceaa9cb459b303350b29a5d to your computer and use it in GitHub Desktop.
Save Zazza/5287170b9ceaa9cb459b303350b29a5d to your computer and use it in GitHub Desktop.
ParseJson with anonymous func + yield
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