Skip to content

Instantly share code, notes, and snippets.

@moinulmoin
Created December 22, 2021 16:42
Show Gist options
  • Select an option

  • Save moinulmoin/dfaa7c5b496d08a4a8031f602b2d2f44 to your computer and use it in GitHub Desktop.

Select an option

Save moinulmoin/dfaa7c5b496d08a4a8031f602b2d2f44 to your computer and use it in GitHub Desktop.
const root = {
a: {
m: { c: { x: 'apple', y: {} } },
n: { 1: {}, 2: {} },
o: {},
},
};
const findPath = (tree, destination) => {
for (let key in tree) {
if (tree.hasOwnProperty(key)) {
if (tree[key] === destination) {
return key;
} else if (typeof tree[key] === 'object') {
let result = findPath(tree[key], destination);
if (result) {
return key + '→' + result;
}
}
}
}
};
console.log(findPath(root, 'apple'));
@moinulmoin

Copy link
Copy Markdown
Author

initial commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment