why dont yo do it like this:
function Directory(p_id, p_name){
this.name = p_name;
this.id = p_id;
this.subdir = [];
}
Directory.prototype.addSubDir(p_directory){
this.subdir.push(p_directory);
}
then somewhere in your code do this:
var arr_struc = ...;//[your data]
var dir_mem = [];
var rootDir = new Directory(0, 'ROOT')
dir_mem.push(rootDir);
for(var i = 0; i < arr_struc.length; i++){
var tmp_directory = new Directory(i+1, arr_struc[i].name)
dir_mem.push(tmp_directory);
if(!arr_struc[i].parent_id)
{ rootDir.addSubDir(tmp_directory) }
else
{ dir_mem[arr_struc[i].parent_id].addSubDir(tmp_directory) }
}
adding some other methods to read subdirectorys by ID or simular and returning "this" you would be able to get subdirectorys by methodchaining ;) pretty OO style but I think its a nice way to structure code
Hope it helped in your special case
EDIT:
here is an example of methodchaining your subdir's:
Directory.prototype.getSubDirs(){
return this.subDir;
}
Directory.prototype.getSubDirById(p_id){
var allSubDirs = this.getSubDirs();
for(var i = 0; i < allSubDirs.length; i++){
if(allSubDirs[i].id === p_id) return allSubDirs[i];
}
return false;
}
Directory.prototype.getSubDirByName(p_name){
var allSubDirs = this.getSubDirs();
for(var i = 0; i < allSubDirs.length; i++){
if(allSubDirs[i].name === p_name) return allSubDirs[i];
}
return false;
}
Then you could do:
rootDir.getSubDirByName('parent').getSubDirByName('child').getSubDirByName('grandchild A');
or something like that :) -crazy