【问题标题】:Includes LIKE multiple values in array在数组中包含 LIKE 多个值
【发布时间】:2020-07-30 05:48:07
【问题描述】:

我想在一个数组中查找多个值。搜索时,我希望能够使用 SQL 中的 LIKE 语句之类的东西。

arr = ['end', 'start_date', 'hello', 'end_dt', 'pub_date']

当我这样做时:

let el = arr.find(a => a.includes('date') || a.includes('dt'));

现在它只返回它找到的第一个值,start_date

我需要它返回:

start_date
end_dt
pub_date

我该怎么做?

【问题讨论】:

  • 使用filter 而不是find

标签: javascript jquery arrays ecmascript-6 include


【解决方案1】:

Array.prototype.find 只返回返回条件的第一个匹配项。

使用Array.prototype.filter:

let arr = ['end', 'start_date', 'hello', 'end_dt', 'pub_date']
console.log(arr.filter(a => a.includes('date') || a.includes('dt')))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2019-03-21
    相关资源
    最近更新 更多