'How to filter through an array of objects and only get the objects whose entries do not equal any entry from another array's objects?
Basically I need to do an 'all except' option for when you are filtering table of products.
I have two arrays: uniqueProducts and selectedFilters.
uniqueProducts contains objects of products that are to be displayed in a table.
selectedFilters contains objects of the pre-selected filters.
Here's what an object of the uniqueProducts array looks like:
{category: 'radio', price: '50', manufacturer: 'sony', production_date: '2020/05/30'}
Here's what the objects of the selectedFilters array look like:
{type: 'manufacturer', value: 'sony', status: true}
{type: 'category', value: 'radio', status: true}
Because the price filter has min and max, I will filter through prices once I have the array of objects ready.
Filtering also does not include production_date.
So, the selectedFilters objects's type property can be either of the two above.
What I need to do is to filter through the uniqueProducts, and only get the objects that do not match any of the type and value of the objects in selectedFilters.
To put it differently, when you select filters for the products, and click 'all except', you have to see all the products that do not match that filter. Hope you get what I mean to say.
I've tried doing something like this:
uniqueProducts.forEach((product)=>{
for(let i = 0; i < selectedFilters.length; i++) {
if(product[selectedFilters[i].type] !== selectedFilters[i].value) {
allExceptArray.push(product)
}
}
})
I've tried nesting for..loops too, and various other conditions, but the issue I keep running into is that, every time a particular property of an object DOES NOT match, it gets pushed into the array, even if the other property of that same object DOES match.
Can somebody help me figure out how to do the logic here?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
