0

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?

3
  • So a is something like {"hello": "world", "d": ["foo", "bar"]}? Commented Nov 5, 2018 at 21:00
  • 2
    do you mean to ask: check if every word in b is contained in the string a? Commented Nov 5, 2018 at 21:14
  • What do you want to archive here? Does a need to include every word given in b or only some? What should happen if b is empty? ... Many questions. Please edit your question so that every corner case becomes clear ;) Commented Nov 5, 2018 at 21:17

3 Answers 3

1

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)}`);
})

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.