use lodash to find substring from array of strings

50,227

Solution 1

You can easily construct an iteratee for some() using lodash's higher-order functions. For example:

_.some(myArray, _.unary(_.partialRight(_.includes, 'orange')));

The unary() function ensures that only one argument is passed to the callback. The partialRight() function is used to apply the 'orange' value as the second argument to includes(). The first argument is supplied with each iteration of some().

However, this approach won't work if case sensitivity matters. For example, 'Orange' will return false. Here's how you can handle case sensitivity:

_.some(myArray, _.method('match', /Orange/i));

The method() function creates a function that will call the given method of the first argument passed to it. Here, we're matching against a case-insensitive regular expression.

Or, if case-sensitivity doesn't matter and you simply prefer the method() approach, this works as well for ES2015:

_.some(myArray, _.method('includes', 'orange'));

Solution 2

Two quick ways to do it - neither uses lodash (sorry)

var found = myArray.filter(function(el){
  return el.indexOf('oranges') > -1;
}).length;

if (found) { // oranges was found }

or as I mentioned in the comment:

var found = myArray.join(',').indexOf('oranges') > -1;
if (found) { // oranges was found }
Share:
50,227
John Paul Vaughan
Author by

John Paul Vaughan

Updated on July 09, 2022

Comments

  • John Paul Vaughan
    John Paul Vaughan almost 2 years

    I'm learning lodash. Is it possible to use lodash to find a substring in an array of strings?

        var myArray = [
        'I like oranges and apples',
        'I hate banana and grapes',
        'I find mango ok',
        'another array item about fruit'
        ]
    

    is it possible to confirm if the word 'oranges' is in my array? I've tried _.includes, _.some, _.indexOf but they all failed as they look at the full string, not a substring

  • John Paul Vaughan
    John Paul Vaughan about 8 years
    Thanks James. These works great. If no-one clarifies a lodash way I'll mark your answer as correct.
  • John Paul Vaughan
    John Paul Vaughan about 8 years
    Thank you for the technique using _.curry, as I am trying to learn the 'lodash approach'
  • John Paul Vaughan
    John Paul Vaughan about 8 years
    Great explanation of several functions, thank you. I prefer these examples over Alexander Yatkevich's helpful implementation of _.curry, as _.method doesn't require the creation of a separate custom function.
  • John Paul Vaughan
    John Paul Vaughan about 8 years
    I've noticed one small query with the case insensitive example. If the word 'Orange' was a variable (that could be any word), would we have to escape it before passing it in? i.e something like this (not tested) var myWord = 'Orange'; _.some(myArray, _.method('match', new RegExp(_.escapeRegExp(myWord), "i")));
  • AsGoodAsItGets
    AsGoodAsItGets over 6 years
    The last method is great for most cases, and it works even in old browsers. No need for filter or some.