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

I want to check if all of the audio file names in unorderedPhrases array exist in the result array that contains the URLs. if there are all exist return true and if-else return false.

Here is what I've tried. I don't know why it returns false all the times!?

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.

if we have this array then it should return false:

const unorderedPhrases = [

    'i was sent',
    'to earth',

];
评论
呐爱
呐爱

You need another level of nesting - checking if the result array includes the phrase won't work (unless a phrase exactly matches one of the result items), because you need to search inside each string of the result array:

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(
    phrase => result.some(
      resultStr => resultStr.includes(phrase)
    )
  );
}

console.log(checkResults())
点赞
评论