【问题标题】:lodash includes - disable strict checkinglodash 包括 - 禁用严格检查
【发布时间】:2021-02-09 02:03:30
【问题描述】:

我有一个这样的数组

let a = ["1","2","3"];
let searchFor = 1;
_includes(a, searchFor); //this returns false

我认为这个返回 false 因为数组 a 中的值是字符串而不是数字。

如何忽略数据类型?

我不想将数组中的那些字符串转换为数字,因为其中的某些值可能真的是字符串。

谢谢。

【问题讨论】:

标签: javascript lodash


【解决方案1】:

您可以使用 Array.some()(或 lodash 的 _.some())和抽象的相等比较 (==) 创建自己的松散包含:

const loostIncludes = (arr, value) => arr.some(v => v == value)
  
console.log(loostIncludes(["1","2","3"], 1))
console.log(loostIncludes(["1","2","3"], "1"))
console.log(loostIncludes(["1","2","3"], 4))

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2020-06-05
    • 2021-12-21
    • 2020-01-21
    相关资源
    最近更新 更多