在数组的数组中搜索,并返回一个数组中的结果



使用下面的数组,我需要按字母进行搜索,如果它找到了元素,它应该返回一个数组

例子
const data= [[
"car",
"plane",
"boat"
],
[
"cartago",
"barcelona",
"los angeles"
],
[
"headphone",
"phone",
"camera",
]
]

if match "ca"必须返回

[car, cartago, camera]

if match "ne"必须返回

[plane, headphone, phone]

平放数组并检查子字符串是否包含在每个元素

const data= [[      "car",      "plane",      "boat"  ],  [      "cartago",      "barcelona",      "los angeles"  ],  [      "headphone",      "phone",      "camera",  ]]
let a = data.flat().filter(v => v.includes('ca'))
console.log(a)
let b = data.flat().filter(v => v.includes('ne'))
console.log(b)

相关内容

  • 没有找到相关文章

最新更新