您好,非常感谢您为此提供的所有帮助。我有两个数组,第一个是没有扩展名的文件名,第二个包含扩展名。我需要匹配文件名并输出匹配项的第三个数组。
ArrayFileName = [one, two, three, three, five, six, ten]
ArrayFileNameWExt = [one.txt, two.txt, three.txt, ten.wmf, eleven.cgm]
输出数组为
NewArrayFileNameWExt = [one.txt, two.txt, three.txt, ten.wmf]
感谢大家的帮助。 玛克辛
最佳答案
这可以通过 filter() 轻松完成在第二个数组上并使用正则表达式来测试每个 filename
是否包含在第一个数组中:
ArrayFileName = [
"one", "two", "three", "three", "five", "six", "ten", "dot.dot"
];
ArrayFileNameWExt = [
"one.txt",
"two.txt",
"three.txt",
"ten.wmf",
"eleven.cgm",
"dot.dot.csv",
"foo",
".",
""
];
let res = ArrayFileNameWExt.filter(x =>
{
let matchs = x.match(/(.*)\.(.*)$/);
return ArrayFileName.includes(matchs && matchs[1]);
});
console.log(res);
关于Javascript用子字符串搜索匹配两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54225369/