gaaaaaa
gaaaaaa

Reputation: 366

How to use the include function for a variable value

I want to check if a string includes another one from b.

a = "some variable value"
b = ["foo", "bar"] 
c = a.includes(b)

How should I do it?

Upvotes: 0

Views: 1302

Answers (3)

rna
rna

Reputation: 461

Its suppose to be:

c = b.includes(a)

whatever element you are checking to suppose to be an argument and includes method calls on Array.

Upvotes: 1

ATOMP
ATOMP

Reputation: 1489

Array.prototype.every is probably the idiomatic way to do this:

const a = "some variable value"
const b = ["foo", "bar"] 
const c = b.every(s => a.includes(s));

Upvotes: 0

Pietro Nadalini
Pietro Nadalini

Reputation: 1800

It's not really clear what you're asking, so if you want to check if the elements in your array are part of the text in your variable a, you will need to iterate over b to validate if each element is in the string of a like this:

a = "some variable value foo"
b = ["foo", "bar"]
b.forEach(x => {
  console.log(`The text '${x}' is in the text '${a}': ${a.includes(x)}`);
})

Upvotes: 1

Related Questions