Created
February 3, 2022 14:39
-
-
Save seyhagithub/1a2dc72263f038b78b6bc5d92a8f9695 to your computer and use it in GitHub Desktop.
Traversal Object Tree
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 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