简体   繁体   English

使用基于另一个数组的对象动态过滤数组

[英]Filter Array with objects based on another array dynamically

Filter Array with objects based on another array dynamically.使用基于另一个数组的对象动态过滤数组。

I need to filter a main array using another array.我需要使用另一个数组过滤主数组。 But the filter array will only contain a few fields or several.但是过滤器数组只会包含几个或几个字段。

    var codes = [{
"title": "lore",
"city": "New York",
"desc": "lorem lorem",
"age": "32"
 },
{
    "title": "lore2 ",
    "city": "Santa Monica",
    "desc": "lorem2",
    "age": "20"
  }

];

let filter = [{
  "city": "New York"
}, {
  "age": "20"
},

...Or more filters. ...或更多过滤器。 This filter is dynamically created by the person clicking on the checkbox.这个过滤器是由点击复选框的人动态创建的。

] ]

This should work这应该工作

 let codes = [{ "title": "lore", "city": "New York", "desc": "lorem lorem", "age": "20" }, { "title": "lore2 ", "city": "Santa Monica", "desc": "lorem2", "age": "20" } ]; let filter = [{ "city": "New York" }, { "age": "20" }]; let res = codes.filter(code => { return filter.every(f => { const [key, value] = Object.entries(f)[0]; return code[key] == value }); }); console.log(res)

You could take a better data structure which is easier to use instead of unknown keys.您可以采用更易于使用的更好的数据结构,而不是未知的密钥。

As result you get object which all keys/values match.结果,您得到 object,所有键/值都匹配。

 const codes = [{ title: "lore", city: "New York", desc: "lorem lorem", age: "32" }, { title: "lore2 ", city: "New York", desc: "lorem2", age: "20" }], filter = [{ key: "city", value: "New York" }, { key: "age", value: "20" }], result = codes.filter(o => filter.every(({ key, value }) => o[key] === value)); console.log(result);

Maybe something like this could work for you?也许这样的事情对你有用?

Basically looping through filters and then also through the seperate filter objects key-value pairs and checking them against the codes.基本上遍历过滤器,然后还遍历单独的过滤器对象键值对,并根据代码检查它们。

 var codes = [{ "title": "lore", "city": "New York", "desc": "lorem lorem", "age": "32" }, { "title": "lore2 ", "city": "Santa Monica", "desc": "lorem2", "age": "20" }]; var filters = [{"city": "New York"}, {"age": "32"}]; function filterByMultipleFilters(data, filters) { return data.filter(item => { // Loop through all filters and check the current item for (const filter of filters) { for (const [key, value] of Object.entries(filter)) { if (;item[key] || item[key];== value) return false; } } return true. }); } console,clear(); let result = filterByMultipleFilters(codes. filters); console.log(result);

Try the following code sample, I think what you want;试试下面的代码示例,我想你想要什么;

try to get filters object directly instead of another array;尝试直接获取过滤器 object 而不是另一个数组;

var codes = [{ "title": "lore", "city": "New York", "desc": "lorem lorem", "age": "32" }, { "title": "lore2 ", "city": "Santa Monica", "desc": "lorem2", "age": "20" } ]; var codes = [{ "title": "lore", "city": "New York", "desc": "lorem lorem", "age": "32" }, { "title": "lore2", " city": "Santa Monica", "desc": "lorem2", "age": "20" } ];

let filters = [{
  "city": "Santa Monica"
}, {
  "age": "20"
}];

const filterCode = {};
filters.forEach(filter => {
  for (var key of Object.keys(filter))
    filterCode[key] = filter[key];
});
var filteredCodes = codes.filter(code => {
  for (var key of Object.keys(filterCode)) {
      if(code[key] !== filterCode[key])
        return false;
  }
  return true;
});

console.log(filteredCodes);控制台日志(过滤代码);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM