我有这个
rows=[
{label1 : "abc", label2 : "bcd"},
{label3 : "cde", label4 : "dce"}
]
我试过了
if(this.query.length > 1){
rows = rows.filter(row=>row.label1.toLowerCase().includes(this.query))
}
但当我开始查询时,我需要搜索所有“行[]”标签,而不仅仅是 1 类似
rows = rows.filter(row=>row.label1.toLowerCase().includes(this.query))&&
rows.filter(row=>row.label2.toLowerCase().includes(this.query))... etc
当我搜索字母“b”时,我希望得到例如 label1 和 label2 结果
最佳答案
您可以将 some
与 Object.values
混合使用以搜索对象中的所有值:
let rows=[
{label1 : "a", label2 : "b"},
{label3 : "c", label4 : "d"}
]
function findInValues(arr, filter){
return arr.filter(obj => Object.values(obj).some(v => v.includes(filter)))
}
console.log(findInValues(rows, 'd'))
console.log(findInValues(rows, 'a'))
console.log(findInValues(rows, 'notHere'))
关于javascript - 如何在所有数组元素中进行搜索筛选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54559879/