use lodash to find substring from array of strings

前端 未结 7 2514
清歌不尽
清歌不尽 2021-02-14 05:03

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 hat         


        
7条回答
  •  感动是毒
    2021-02-14 05:23

    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 }
    

提交回复
热议问题