Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains all the elements of an array in Javascript

Tags:

javascript

I have a string apple_mango_banana and an array containing [apple,mango].

I want to check if my string contains all of the elements present in the array, if so, I want to show a div with the same ID as my string.

like image 219
Kritika Avatar asked Dec 02 '22 14:12

Kritika


1 Answers

Use every() function on the arr and includes() on the str; Every will return true if the passed function is true for all it's items.

var str = 'apple_mango_banana';
var arr = ['apple','banana'];

var isEvery = arr.every(item => str.includes(item));

console.log(isEvery);
like image 83
Suren Srapyan Avatar answered Jan 19 '23 00:01

Suren Srapyan