Create Tree Data Structure Javascript Substring Indexof