【问题标题】:how to find if string not exist in array nodejs如何查找数组nodejs中是否不存在字符串
【发布时间】:2020-06-21 16:30:07
【问题描述】:

我有一个字符串数组,我想检查 str1 flag = true 中是否不存在 str2

symbols=["A","B","C"];
symbol="D"
if (symbols.indexOf(symbol) === false)
                flag = true;

【问题讨论】:

    标签: arrays node.js string


    【解决方案1】:

    使用includes:

    if (!symbols.includes(symbol)) {
      // symbol is not in symbols
    }
    

    如果合适,您也可以使用findindexOf

    【讨论】:

      【解决方案2】:

      当 IndexOf 没有找到符号时,它返回 -1。 所以你的代码应该是

      if (symbols.indexOf(symbol) === -1)
      

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        相关资源
        最近更新 更多