我想检查包含URL的unorderedPhrases
数组中是否存在数组中的所有音频文件名result
。如果都存在returntrue
和if-else return false
。
这是我尝试过的。我不知道为什么它总是返回false
!
let result = [
"https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
"https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
"https://example.com/test/unordered/i was sent to earth to protect you_A/to protect you.mp3",
];
const unorderedPhrases = [
'i was sent',
'to earth',
'to protect you'
];
function checkResults(){
return unorderedPhrases.every(r=> result.includes(r));
}
console.log(checkResults())
上面的代码应该返回true,因为数组中unorderedPhrases
存在所有音频文件result
。
如果我们有这个数组,那么它应该返回,false
因为其中有一个unorderedPhrases
不存在的项目result
:
let result = [
"https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
"https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
];
如果要忽略文件夹结构,而仅考虑最后的内容,则可以先从每个result
项目中删除文件夹部分,将其变成另一个数组,然后遍历短语,检查数组是否没有文件夹部分包括短语加.mp3
:
let result = [
"https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
"https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
"https://example.com/test/unordered/i was sent to earth to protect you_A/to protect you.mp3",
];
const resultWithoutFolders = result.map(str => str.split('/').pop());
const unorderedPhrases = [
'i was sent',
'to earth',
'to protect you'
];
function checkResults() {
return unorderedPhrases.every(
phrase => resultWithoutFolders.includes(phrase + '.mp3')
);
}
console.log(checkResults())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句