Last active
August 28, 2018 13:05
-
-
Save sarathsp06/732c71f22aee26fa89975b9fb75d03dd to your computer and use it in GitHub Desktop.
Convert a string with dots('.') to a tree structure
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
var lst = function(array){return array.slice(-1);} | |
var head = function(array){return array.slice(0,-1);} | |
function CreateTree(key){ | |
keys = key.split('.') | |
tags = {} | |
var last = lst(keys) | |
var hd = head(keys) | |
while(hd.length!==0){ | |
tags[hd.join(".")] = last[0] | |
last = lst(hd) | |
hd = head(hd) | |
} | |
return tags | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment