You can do this with lodash, but it is also very convenient using your own javascript methods:
function stringArrayContains(array, str) { function contains(el) { return (el.indexOf(str) !== -1) ? true : false; } return array.some(contains); }
Testing the specified function:
var a = ['hello', 'there']; var b = ['see', 'ya', 'later']; stringArrayContains(a, 'ell') // true stringArrayContains(a, 'what') // false stringArrayContains(b, 'later') // true stringArrayContains(b, 'hello') // false
Array.prototype.some applies the function that you define for each element of the array. This function (named contains in our case) should return true or false. Iterating through array elements, if any of the elements returns true, some method returns true.
Personally, I think that in general, if you can use your own JS methods for simple functions, it is preferable to load the library to do the same. Lodash does have performance advantages, but they are not necessarily implemented unless you are processing large amounts of data. Only my two cents.
Hooray!
source share