-2

I want to create an if condition that check if the string contains two keywords for example i have this str: initcall7773107b-7273-464d-9374-1bff75accc15TopCenter how check if this str contains : initcall && TopCenter in addition there is another string must added to condition so the scenario will be like this if(first_str.includes('initcall','TopCenter') && second_str.includes('start', 'BottomLeft') { // Do somthig })

after searching i found that how to check if one keyword includes in the string not two or using regExp, so i need to check for two words in one string and add another parameter to the condition as mentioned above.

Eslam Magdy
  • 73
  • 3
  • 14
  • 1
    Does this answer your question? [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – Tân Jun 18 '20 at 13:56
  • unfortunately no, in my case i want to check for two keywords in each string in one condition – Eslam Magdy Jun 18 '20 at 14:04

3 Answers3

1

This statement sub1.map(string => str.includes(string)).every(Boolean) is basically taking every string from the sub1 array and checking if it is included in the original string str creating an array of booleans, the .every() evaluates the array of booleans by calling the boolean function that evaluates the whole statement to a single boolean.

  var str = "initcall7773107b-7273-464d-9374-1bff75accc15TopCenter";
  var sub1 = ["initcall","TopCenter"];
  var sub2 = ["start","BottomLeft"]

  var n = sub1.map(string => str.includes(string)).every(Boolean) && sub2.map(string => str.includes(string)).every(Boolean);

  console.log(n);

reference to .every() function & Boolean function

mahmoudayoub
  • 586
  • 4
  • 6
0

I can only think of this:

'use strict';

String.prototype.includes = function (...args) {  
  return args.filter(str => this.indexOf(str) > -1).length === args.length;
};

var str = 'initcall7773107b-7273-464d-9374-1bff75accc15TopCenter';
if(str.includes('initcall', 'TopCenter')) {
  console.log('Do something...');
}
Tân
  • 1
  • 15
  • 56
  • 102
0

.every() will stop processing immediately if the method passed to it returns false. The above suggestion does the check in the method passed to .map(), which runs on every element unconditionally. Why not use just .every() so that negative results are found faster?

Here's some injection that will demonstrate:

let counter;
const includesAllWithMap = (s, ...args) => args.map((v) => {
  counter += 1;
  return s.includes(v);
}).every(Boolean);

const includesAllWithJustEvery = (s, ...args) => args.every((v) => {
  counter += 1;
  return s.includes(v);
});

counter = 0;
rc = includesAllWithMap('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c');
console.log('result with map =', rc, 'number of comparisons =', counter);
// result with map = false number of comparisons = 6

counter = 0;
rc = includesAllWithJustEvery('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c');
console.log('result with just every =', rc, 'number of comparisons =', counter);
// result with map = false number of comparisons = 3

To make it succinct:

const includesAll = (s, ...args) => args.every(v => s.includes(v));
> includesAll('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c')
// false
> includesAll('abcd', 'a', 'd')
// true

You can make a slight change if you want to provide the substrings in arrays, as the original poster wanted:

const includesAll = (s, ...args) => args.flat().every(v => s.includes(v));
> includesAll('abcdef', ['ab', 'cd', 'ba'], ['ef', 'a', 'c'])
// false
> includesAll('abcd', ['a', 'd'])
// true
> includesAll('abcdef', ['ab', 'abc'], ['de'], ['bcd'])
// true

And if you want both:

const includesAll = (s, ...args) => args.every(v => s.includes(v));
const includesAllinArrays = (s, ...args) => includeAll(s, args.flat());

And if you want an all-in-one (at the sacrifice of some efficiency):

const includesAll = (s, ...args) => args
  .map(a => Array.isArray(a) ? a.flat() : [a])
  .flat()
  .every(v => s.includes(v));
> includesAll('abcdef', 'ab', 'abc', ['d'], ['d', 'e'], 'cd')
// true
blackcatweb
  • 1,003
  • 1
  • 10
  • 11