Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
How can I get the item that matches the criteria using some() and includes()?

I have an array of strings. I want to check if any of those strings is present in one sentence, so I used some() and includes(), and got a boolean telling me if any of the strings are present. So far so good.

Now, I need to know which of those strings is the one included in the sentence.

Here's my code:

var fruits = [
"apple",
"pear",
"orange",
"banana",
"grapes",
];

var text = "I want to eat a banana";

if (fruits.some(v => text.includes(v))) {
console.log("One of the fruits is in the sentence!");
}

I would need to get the value of that 'v', but I can't find a way to do it. Any ideas??

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use find if its just one occurrence or filter if you want multiples:

var fruits = [
"apple",
"pear",
"orange",
"banana",
"grapes",
];

var text = "I want to eat a banana and an orange";

var foundFind = fruits.find(fruit => text.includes(fruit))
console.log("foundFind", foundFind);


var foundFilter = fruits.filter(fruit => text.includes(fruit))
console.log("foundFilter", foundFilter);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!