Created
December 22, 2021 16:42
-
-
Save moinulmoin/dfaa7c5b496d08a4a8031f602b2d2f44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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')); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
initial commit