Skip to content

Instantly share code, notes, and snippets.

@cagataycali
Last active November 22, 2021 15:38
Show Gist options
  • Save cagataycali/c5083c35493b34794dcf731a305d2a13 to your computer and use it in GitHub Desktop.
Save cagataycali/c5083c35493b34794dcf731a305d2a13 to your computer and use it in GitHub Desktop.
[JavaScript] Hashmap of hashmaps
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