假设我有一个像这样的文件夹结构:
我有一个包含所有路径的数组:
let paths = ['/root/','/root/folder1/','/root/folder2','/root/folder2/folder3'...
我想要完成的是删除递归子项的父目录,以便我最终得到这个:
let paths = ['/root/folder1/folder4','/root/folder2/folder3/folder5','/root/folder6'];
不要担心这不是家庭作业或任何事情,只是我被困在我的个人项目上:D
答案 0 :(得分:1)
您可以检查数组是否包含以给定路径开头的字符串。然后过滤掉该项目。
var paths = ['/root/', '/root/folder1/', '/root/folder2', '/root/folder2/folder3/', '/root/folder1/folder4', '/root/folder2/folder3/folder5'],
result = paths.filter((p, i, a) => !a.slice(i + 1).some(o => o.startsWith(p)));
console.log(result);

答案 1 :(得分:1)
我使用filter
&以不同方式尝试了它而是some
API
let paths = ['/root/', '/root/folder1/', '/root/folder1/folder4/', '/root/folder2', '/root/folder2/folder3', , '/root/folder2/folder3/folder5', , '/root/folder6/'];
console.log(paths.filter((path) => {
return !paths.some((indexPath) => { return path !== indexPath && indexPath.indexOf(path) > -1 });
}));