【问题标题】:How to filter javasciptarray based on a filter object如何根据过滤器对象过滤 javasciptarray
【发布时间】:2021-04-02 01:58:05
【问题描述】:

基于过滤器对象,我需要过滤掉数据。下面是具有所需输出的过滤器对象和示例数据。过滤器对象是通过 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
  }]

【问题讨论】:

  • 尝试为它写一个函数。
  • @KooiInc 好吧,我正在尝试,但无法找到解决方案。以下函数仅适用于过滤器对象中的单个值。 data.filter(function(item) { for (var key in filter) { if (item[key].toLowerCase().indexOf(filter[key].toLowerCase()) == -1){ return false; } }返回真;});
  • 那么,你为什么不把它包括在你的问题中呢?这将为某人提供更多线索来回答它。 This 可能会提供有关如何提出好问题的信息。 Or this.
  • 我同意。我会改进的

标签: javascript arrays object filter


【解决方案1】:

我开始使用更通用的版本,您可以在其中添加过滤器对象的属性。

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
  }
];

let results;

if (Object.keys(filter).length === 0)
  results = data;
else {
  results = data.filter((item) => {
      return Object.keys(filter).find(key => {
          return item[key] && item[key].toLowerCase().includes(filter[key].toLowerCase());
      });
  });  
}

console.log(results);

【讨论】:

  • 这在过滤器对象为空时不起作用。如果过滤器对象为空,它必须返回所有数据。
  • 那你只需添加一个if语句并检查filter是否有属性。我已经更新了我的答案。
  • @Nishanth 不要忘记接受我的回答,如果它解决了你的问题,请点赞,这样我就可以释放一些催产素。
  • @marcos 完成好友
【解决方案2】:

在 ES6 中你可以这样做

var results = data.filter(function(item)=>{
   return item.city === filter.city || item.hospname === filter.hospname;
});

编码愉快!

【讨论】:

  • 过滤器对象可能没有值或一个或多个值。因此,他的解决方案需要是动态的。
【解决方案3】:

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; }

【讨论】:

  • 可以是动态的而不是过滤器对象的硬编码值吗?
  • 它不是硬编码的..我使用了filter变量。
  • 您可以根据您的用例更改变量名称。
  • 我知道,但是他的过滤器对象在任何给定时间都可能有零个或多个值。因此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2018-08-05
  • 2022-11-30
  • 2021-04-15
相关资源
最近更新 更多