【问题标题】:How do you check if a string contains strings from two tables in Node.js?如何检查一个字符串是否包含来自 Node.js 中两个表的字符串?
【发布时间】:2018-03-10 20:21:30
【问题描述】:

当消息包含一个问候语和一个名字时,我希望控制台打印 true。

例如。 1 条消息 = ['嘿约翰'] //true

例如。 2 message = ['yo johnny'] //假

例如。 3 message = ['hello johnny'] //true

例如。 4 message = ['hey evan'] //假

var message = ['']

var greetings = ['hi', 'hello', 'hey']
var names = ['john', 'johnny']

if (message.includes(greetings) && message.includes(names)) {
    console.log(true)
}

【问题讨论】:

    标签: node.js string if-statement


    【解决方案1】:

    你可以这样检查:

    const greetings = ['hi', 'hello', 'hey']
    const names = ['john', 'johnny']
    const isIncludes = (msg, arr) => {
      for (let i in arr) {
        if (msg.includes(arr[i])) {
          return true
        }
      }
      return false;
    }
    
    message = ['hey john'] //true
    console.log(isIncludes(message[0], greetings) && isIncludes(message[0], names))
    
    message = ['yo johnny'] //false
    console.log(isIncludes(message[0], greetings) && isIncludes(message[0], names))
    
    message = ['hello johnny'] //true
    console.log(isIncludes(message[0], greetings) && isIncludes(message[0], names))
    
    message = ['hey evan'] //false
    console.log(isIncludes(message[0], greetings) && isIncludes(message[0], names))
    
    message = [''] //false
    console.log(isIncludes(message[0], greetings) && isIncludes(message[0], names))

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 2021-05-06
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多