Last active
November 22, 2021 15:38
-
-
Save cagataycali/c5083c35493b34794dcf731a305d2a13 to your computer and use it in GitHub Desktop.
[JavaScript] Hashmap of hashmaps
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
function generate(...input) { | |
const tree = {'': {}}; | |
for (const text of input) { | |
let currentNode = tree['']; | |
Array.from(text).forEach(char => { | |
if (!currentNode[char]) { | |
currentNode[char] = {}; | |
} | |
currentNode = currentNode[char] | |
}) | |
} | |
return tree; | |
} | |
generate('cagatay', 'cali') | |
// {"":{"c":{"a":{"g":{"a":{"t":{"a":{"y":{}}}}},"l":{"i":{}}}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment