javascript - 如何在所有数组元素中进行搜索筛选

标签 javascript

我有这个

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 结果

最佳答案

您可以将 someObject.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/

相关文章:

javascript - 使用 Javascript 选择特定单词?

javascript - 用于 Javascript Web 应用程序开发的类似于 ASP.net 用户控件的东西?

javascript - 动态添加时,jQuery remove() 不适用于 div 标签

javascript - 将 .json 模型导入 Three.js

javascript - 如何在文件输入更改事件中使用循环

javascript - 子 socket 中的 Angular 子路线

javascript - 为什么我得到空表格?如何自动创建 Iniline 模型?

javascript - 刷新wheelnav.js中的项目

javascript - 按钮 onclick 函数触发两次

javascript - 传递键值数组 AJAX