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
  • 133,658
  • 13
  • 134
  • 193
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 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,912
  • 2
  • 17
  • 30
  • 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