简体   繁体   English

检查数组的所有元素是否包含在另一个数组中

[英]Check if all elements of array includes inside another array

I want to check if all of the audio file names in unorderedPhrases array exist in the result array that contains the URLs.我想检查unorderedPhrases数组中的所有音频文件名是否存在于包含 URL 的result数组中。 if there are all exist return true and if-else return false .如果全部存在则返回true并且 if-else 返回false

Here is what I've tried.这是我尝试过的。 I don't know why it returns false all the times??我不知道为什么它总是返回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())

The above code should return true because all the audio files in unorderedPhrases exist in the result array.上面的代码应该返回 true,因为unorderedPhrases中的所有音频文件都存在于result数组中。

if we have this array then it should return false because there is a item in unorderedPhrases that doesn't exist in 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",
];

If you want to ignore the folder structure and only consider what comes at the very end, you can remove the folder part from each result item first, turning it into another array, then when iterating over the phrases, check if the array without the folder part includes the phrase plus .mp3 :如果您想忽略文件夹结构而只考虑最后出现的内容,您可以先从每个result项中删除文件夹部分,将其转换为另一个数组,然后在遍历短语时,检查没有文件夹的数组部分包括短语 plus .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())

It returns false because result is a 2D array.它返回 false 因为result是一个二维数组。 That means your result has 3 elements, which are those 3 strings, and each string is an array consisting of characters.这意味着您的result有 3 个元素,即这 3 个字符串,每个字符串都是一个由字符组成的数组。

EDIT: Sorry for the mis-assumption.编辑:对不起,错误的假设。 TJ Crowder has pointed out for me. TJ Crowder 为我指出了这一点。 It's not an 2D array.它不是二维数组。 The strings act like array too.字符串也像数组一样。 So it can be accessed like a 2D array所以它可以像二维数组一样被访问

This would be a more correct snippet:这将是一个更正确的片段:

 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[0].includes(r)) } console.log(checkResults())

If you want to check all the 3 strings in result you have to loop over it.如果要检查result中的所有 3 个字符串,则必须对其进行循环。 At each time, check the unorderedPhrases like above每次都检查上面的unorderedPhrases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM