1.要生成的大概結構:
var treedata = [,],
},,
],},,]
}];
2.資料:
const arr = ['test/', 'test/a.txt', 'test/cmp/', 'test/cmp/b.txt', 'demo/', 'demo/b.txt']
3.**實現:
const arr = ['test/', 'test/a.txt', 'test/cmp/', 'test/cmp/b.txt', 'demo/', 'demo/b.txt']
// 不考慮root:"/";
// 檔名中不存在"/"
// 同層級中,目錄和檔案不可同名
const treeify = (nodelist) => ;
// 生成節點資訊按層級
const nodeinlevel =
nodelist.foreach(t =>
const key = (math.random() + '').split('.')[1] * 1
const title = isdir ? pathinfolist[pathinfolist.length - 2] : pathinfolist[pathinfolist.length - 1]
const level = isdir ? pathinfolist.length - 2 : pathinfolist.length - 1
// collet it
nodeinlevel[level] = nodeinlevel[level] ||
nodeinlevel[level].push()
});// 廣度排,按每層級
const getnodecot = (node, level, root) => = node;
let curcot = null;
if (level === 0) else cant find parent $`,);}
return pat.children
}return curcot
};const maxlevel = nodeinlevel.length;
for (let level = 0; level < maxlevel; level++) )
}return root;
};const r = treeify(arr);
console.log(r)
6.如果只返回檔案路徑,不返回目錄路徑,比如這樣:
const newarr = ['test/data/aa.html', 'test/ddd/a.txt', 'test/cmp/ccc.txt', 'test/cmp/b.txt', 'demo/ccc/tttt/demo.html', 'demo/b.txt']
7.寫乙個方法解析出目錄路徑
function getcatalogpath(nodelist) else
let flag = false;
if (newnodelist.length > 0)
}if (!flag)
} else }}
} else
}return newnodelist;
}console.log(getcatalogpath(newarr))
JavaScript獲取當前根目錄
主要用到location 物件,包含有關當前 url 的資訊,是 window 物件的乙個部分,可通過 window.location 屬性來訪問。方法一 window.document.location.href window.document.location.pathname 網路 funct...
JavaScript獲取當前根目錄
主要用到location 物件,包含有關當前 url 的資訊,是 window 物件的乙個部分,可通過 window.location 屬性來訪問。方法一 window.document.location.href window.document.location.pathname 網路 funct...
php根據檔案目錄獲取資料夾目錄
檔案目錄資料 filedirdate array 0 c ggg aaa bbb ccc ddd.php 1 c ggg aaa ccc ddd fff.php 2 c ggg aaa ddd fff eee.lib 迴圈處理資料 foreach filedirdate as key value f...