javascript - 如何根据过滤器对象过滤 javasciptarray
问题描述
基于过滤器对象,我需要过滤掉数据。下面是具有所需输出的过滤器对象和示例数据。过滤器对象是通过 ui 中的多搜索组件动态生成的。然后当用户点击搜索时,需要过滤数据。
var filter = {
city: 'pana',
hospname: 'sara'
};
var data = [
{
"city": "Hot Springs",
"hospname": "St. Vincent Hot Springs",
"version": "VA48A",
"sysid1": "CT67400",
"type": "CompressedFile",
"rowIndex": 0,
"selected": false,
"disabled": true
},
{
"city": "Panama City",
"hospname": "Bay Medical Center",
"version": "VA48A",
"sysid1": "CT67399",
"type": "CompressedFile",
"rowIndex": 1,
"selected": false,
"disabled": true
},
{
"city": "Sarasota",
"hospname": "Sarasota Memorial Hospital",
"version": "VA44A",
"sysid1": "C7393",
"type": "CompressedFile",
"rowIndex": 2,
"selected": false,
"disabled": true
},
{
"city": "DAVENPORT",
"hospname": "Genesis Medical Center",
"version": "VA48A",
"sysid1": "C6333",
"type": "CompressedFile",
"rowIndex": 6,
"selected": false,
"disabled": true
}
];
预期输出:
[{
"city": "Panama City",
"hospname": "Bay Medical Center",
"version": "VA48A",
"sysid1": "CT67399",
"type": "CompressedFile",
"rowIndex": 1,
"selected": false,
"disabled": true
},
{
"city": "Sarasota",
"hospname": "Sarasota Memorial Hospital",
"version": "VA44A",
"sysid1": "CT67393",
"type": "CompressedFile",
"rowIndex": 2,
"selected": false,
"disabled": true
}]
解决方案
var filter = {
city: 'pana',
hospname: 'sara'
};
var data = [
{
"city": "Hot Springs",
"hospname": "St. Vincent Hot Springs",
"version": "VA48A",
"sysid1": "CT67400",
"type": "CompressedFile",
"rowIndex": 0,
"selected": false,
"disabled": true
},
{
"city": "Panama City",
"hospname": "Bay Medical Center",
"version": "VA48A",
"sysid1": "CT67399",
"type": "CompressedFile",
"rowIndex": 1,
"selected": false,
"disabled": true
},
{
"city": "Sarasota",
"hospname": "Sarasota Memorial Hospital",
"version": "VA44A",
"sysid1": "C7393",
"type": "CompressedFile",
"rowIndex": 2,
"selected": false,
"disabled": true
},
{
"city": "DAVENPORT",
"hospname": "Genesis Medical Center",
"version": "VA48A",
"sysid1": "C6333",
"type": "CompressedFile",
"rowIndex": 6,
"selected": false,
"disabled": true
}
];
const result=data.filter(item=>item.city.toLowerCase().includes(filter.city.toLowerCase()) || item.hospname.toLowerCase().includes(filter.hospname.toLowerCase()) )
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- rest - 使用linkedin API v2获取粉丝数
- c# - Azure SignalR .NET Framework C# 客户端无法进行身份验证
- ios - 将 JSON 数组字符串转换为 JSON 数组返回 null
- python - 使用 shutil 复制会在 csv 数据上中断循环
- java - 使用 volley 在发布请求正文中使用多部分数据将图像上传到服务器
- ruby-on-rails - 使用传递的值更新参数
- javascript - 寻找一个正则表达式来只获取我想要的字段
- javascript - 来自已发布表单的变量在弹出框(javascript)后停止或继续使用 php 代码
- jquery - 使用 Ajax 将点击链接的 href 和 id 从页面发送到 django
- visual-studio - 入住问题