js从一组相对路径中筛选出n组文件名相同的路径。n为1组:xxaataatxxxxaat
你可以使用JavaScript的数组方法 filter() 和字符串方法 split() 来实现这个功能。
首先,将相对路径字符串存储在一个数组中,然后使用 filter() 方法来筛选出文件名相同的路径。
下面是一个示例代码:
const paths = [
'/xx/aa.t',
'/aa.t',
'/xx/xx/aa.t',
];
const getPathsByFileName = (paths, fileName) => {
return paths.filter(path => {
const splitPath = path.split('/');
const pathFileName = splitPath[splitPath.length - 1];
return pathFileName === fileName;
});
};
const fileName = 'aa.t';
const filteredPaths = getPathsByFileName(paths, fileName);
console.log(filteredPaths);
输出结果为:
[ '/xx/aa.t', '/aa.t', '/xx/xx/aa.t' ]
这样就可以筛选出文件名为 aa.t 的所有路径
原文地址: https://www.cveoy.top/t/topic/iyyV 著作权归作者所有。请勿转载和采集!