0

how to check if a string includes all array elements, regardless of element's position inside the string

for example:

var arr = ['lorem', 'ipsum', 'dolor'];
var str = 'lorem blue dolor sky ipsum';

I need something like this:

if(str.includesAll(arr)){console.log('string includes all elements');}
else{console.log('string does not include all elements');}
Jamiec
  • 128,537
  • 12
  • 134
  • 188
qadenza
  • 8,611
  • 18
  • 61
  • 108
  • Here is one I wrote just for you https://jsfiddle.net/mplungjan/3rdcezmg/ - it splits the sentence and checks each word in the sentences instead of the array item in the sentence – mplungjan Jan 20 '22 at 11:09

1 Answers1

1

You could try to use Array.prototype.every to call your check function on every element.

var arr = ['lorem', 'ipsum', 'dolor'];
var str = 'lorem blue dolor sky ipsum';

console.log(arr.every(el => str.includes(el)));
Krzysztof Krzeszewski
  • 5,299
  • 2
  • 14
  • 27
  • 1
    problem - if str is for example - `sky loremdoloripsum blue`. I need to check only word by word – qadenza Jan 20 '22 at 10:43