【发布时间】:2021-07-03 23:04:59
【问题描述】:
我想过滤掉一些在数组中提到但不知道怎么做的文件
我看过一篇文章,但它只显示了一个扩展如何过滤它如果有一个扩展数组怎么办
async function getAllFile(folderPath, depth) {
let files = await fs.readdir(folderPath);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory() && depth > 0) {
return getAllFile(filePath, depth - 1);
} else if (stats.isFile()) return filePath;
})
);
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
const filenames = new Set([
".html",
".htm",
".aspx"
])
const filterFiles = async (folderPath) => {
let filename,
parts;
const paths = await getAllFile(folderPath);
const filteredFiles = [];
const otherFiles = [];
for (const filePath of paths) {
parts = filePath.split("/");
filename = parts[parts.length - 1];
if (filenames.has(filename.toLowerCase())) {
filteredFiles.push(filePath);
} else {
otherFiles.push(filePath);
}
}
return { filteredFiles, otherFiles };
};
任何解决方案如何解决这个问题
【问题讨论】:
-
没有@CyberEternal
-
您在此处发布的代码与上面帖子中的代码完全相同,并且是我的代码。实际上,我不明白你需要什么。你也可以发布一个例子吗?比如你想在什么时候收到什么。
-
是的,它只是您的代码,但我希望在第二个数组
OtherFiles: abc.txt, pqr.pdf中的FilterFiles: index.html, default.htm中的 3 个数组中输出,但在第三个数组中,即ignoreFIles: abc.html, pqr.aspx, xyz.htm所有的 .html、.htm、.aspx 中没有进入 filterFile 应该进入 ignoreFile @CyberEternal -
请看我的回答@Praveen。
标签: javascript node.js promise callback