Home > Enterprise >  Filter Array with objects based on another array dynamically
Filter Array with objects based on another array dynamically

Time:09-30

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.

]

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

Try the following code sample, I think what you want;

try to get filters object directly instead of another array;

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

CodePudding user response:

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.

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

  • Related