0

I have set lists

example:

const answerList = [{index: 2, answer: nice}, {index: 5, answer: sunday} ...] 

like that

and I have a sentence

example:

"hi i'm theo nice to meet you. how are you"

so I want to check if there is a correct answer in the sentence and if find it, return answerList set

example:

return value is [{index: 2, answer: nice}]

Because there is the word "nice" in the sentence.

Can someone tell me a good way?

Dale K
  • 25,246
  • 15
  • 42
  • 71
정태현
  • 29
  • 2
  • 1
    Can you clarify more, you want to see from the list of words, which ones are in the sentence? – Dvid Silva Mar 27 '20 at 01:48
  • Too similar to this, https://stackoverflow.com/questions/5582574/how-to-check-if-a-string-contains-text-from-an-array-of-substrings-in-javascript – Dvid Silva Mar 27 '20 at 01:49

1 Answers1

2

You can try using Array.prototype.filter() and String.prototype.includes():

const answerList = [{index: 2, answer: 'nice'}, {index: 5, answer: 'sunday'}];
const sentence = "hi i'm theo nice to meet you. how are you";
var res = answerList.filter(a => sentence.includes(a.answer));
console.log(res);
Mamun
  • 66,969
  • 9
  • 47
  • 59