lodash _.contains one of multiple values in string

前端 未结 3 2097
予麋鹿
予麋鹿 2021-02-13 02:17

Is there a way in lodash to check if a strings contains one of the values from an array?

For example:

var text = \'this is some sample text\';
var values         


        
3条回答
  •  臣服心动
    2021-02-13 02:50

    No. But this is easy to implement using String.includes. You don't need lodash.

    Here is a simple function that does just this:

    function multiIncludes(text, values){
      return values.some(function(val){
        return text.includes(val);
      });
    }
    
    document.write(multiIncludes('this is some sample text',
                                 ['sample', 'anything']));
    document.write('
    '); document.write(multiIncludes('this is some sample text', ['nope', 'anything']));

提交回复
热议问题