Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash: Search a string for at least 1 value in an array

Is there a lodash function that takes an array of needles, and searches a string (haystack) for at least one match? For example:

let needles = ['one', 'two', 'three']
let str = 'one in every thousand will quit their jobs'

I need to search str to see if it contains at least one of the needles. I can implement this without lodash, but if there's a function that will help out I'd rather not reinvent the wheel as I already have lodash loaded into my project.

like image 730
BugHunterUK Avatar asked Oct 26 '25 10:10

BugHunterUK


1 Answers

Use Array#some, The some() method tests whether some element in the array passes the test implemented by the provided function.

let needles = ['one', 'two', 'three']
let str = 'one in every thousand will quit their jobs';

let bool = needles.some(function(el) {
  return str.indexOf(el) > -1;
});
console.log(bool);
like image 78
Rayon Avatar answered Oct 28 '25 22:10

Rayon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!