Skip to content

Instantly share code, notes, and snippets.

@seyhagithub
Created February 3, 2022 14:39
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 seyhagithub/1a2dc72263f038b78b6bc5d92a8f9695 to your computer and use it in GitHub Desktop.
Save seyhagithub/1a2dc72263f038b78b6bc5d92a8f9695 to your computer and use it in GitHub Desktop.
Traversal Object Tree
function traverse(data: any) {
const results: any[] = []
// let identifer = nanoid()
const recursive = (obj: any, pre: any = {}) => {
_.forIn(obj, function (val, key) {
// base case
if (key.toLowerCase().indexOf('remark') > 0) {
pre = {key, val}
}
if (key == 'thumbUrl') {
const last = _.last(results)
console.log('last', { key, val, parent: pre.key, pre })
results.push({ key, val, parent: pre.key, pre })
}
// recursive case
if (_.isArray(val)) {
val.forEach(function (el) {
if (_.isObject(el)) {
recursive(el, pre)
}
})
}
else if (_.isObject(val)) {
recursive(obj[key], pre)
}
})
}
recursive(data)
console.log('hello data', results)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment