Reputation: 322
I'm trying to filter an array of objects using a 'filters' object. The 'filters' object below should filter the array for object with type: 'toys' OR 'electronics', language: 'English' OR 'Spanish'. I'm trying to create a function that would return the last object (name: 'StoreF'
) based on the provided filter as an example.
var filters = {
country: ["France"],
type: ["toys", "electronics"],
language: ["English", "Spanish"]
}
var stores = [{
name: "StoreA",
country: "United States",
type: ["toys", "groceries"],
language: ["English", "Spanish"]
},
{
name: "StoreB",
country: "Spain",
type: ["toys"],
language: ["English", "Spanish"]
},
{
name: "StoreC",
country: "France",
type: ["autoparts"],
language: ["French"]
},
{
name: "StoreD",
country: "Thailand",
type: ["toys"],
language: ["Thai"]
},
{
name: "StoreE",
country: "India",
type: ["toys"],
language: ["English"]
},
{
name: "StoreF",
country: "France",
type: ["toys"],
language: ["English", "French"]
},
]
Using this function works fine until I introduce 2 filters for the same category (i.e language: ["English", "Spanish"]
).
function nestedFilter(targetArray, filters) {
var filterKeys = Object.keys(filters);
return targetArray.filter(function(eachObj) {
return filterKeys.every(function(eachKey) {
if (!filters[eachKey].length) {
return true;
}
if (!$.isEmptyObject(eachObj[eachKey])) {
return eachObj[eachKey].includes(filters[eachKey]);
}
});
});
};
nestedFilter(stores, filters);
What am I doing wrong?
Upvotes: 0
Views: 67
Reputation: 73241
For OR you need some
not every
.
const res = stores.filter(store =>
Object.entries(filters).every(([key , value]) => value.some(e =>
store[key].includes(e)
)));
console.log(res);
<script>
const filters = {
country: ["France"],
type: ["toys", "electronics"],
language: ["English", "Spanish"]
};
const stores = [
{
name: "StoreA",
country: "United States",
type: ["toys", "groceries"],
language: ["English", "Spanish"]
},
{
name: "StoreB",
country: "Spain",
type: ["toys"],
language: ["Engilsh", "Spanish"]
},
{
name: "StoreC",
country: "France",
type: ["autoparts"],
language: ["French"]
},
{
name: "StoreD",
country: "Thailand",
type: ["toys"],
language: ["Thai"]
},
{
name: "StoreE",
country: "India",
type: ["toys"],
language: ["Engilsh"]
},
{
name: "StoreF",
country: "France",
type: ["toys"],
language: ["English", "French"]
},
];
</script>
Also note the multiple typos in your stores, Engilsh won't return true for English
Upvotes: 3