Skip to content

Instantly share code, notes, and snippets.

@sumitk
Forked from stomg7969/fianlMultiFilter.js
Last active July 29, 2019 11:05
Show Gist options
  • Save sumitk/f15d0d39e77f6f6addc6252bdbebaa7e to your computer and use it in GitHub Desktop.
Save sumitk/f15d0d39e77f6f6addc6252bdbebaa7e to your computer and use it in GitHub Desktop.
FILTER BLOG - multi filter
https://medium.com/better-programming/creating-a-multi-filter-function-to-filter-out-multiple-attributes-javascript-react-rails-5aad8e272142
const category = ['innerwear', 'dress', 'robe', 'pajamas', 'sweater', 'pants'];
const color = ['white', 'black', 'brown', 'navy', 'blue', 'yellow', 'pink', 'purple', 'beige', 'red', 'green'];
const gender = ['unisex', 'girl', 'boy'];
const material = ['modal', 'cotton', 'spandex', 'tencel', 'rayon'];
const collectedTrueKeys = {
color: ['white', 'pink'],
gender: ['girl'],
material: ['cotton'],
cateogry: []
}
multiPropsFilter = (products, filters) => {
const filterKeys = Object.keys(filters);
return products.filter(product => {
return filterKeys.every(key => {
if (!filters[key].length) return true;
// Loops again if product[key] is an array (for material attribute).
if (Array.isArray(product[key])) {
return product[key].some(keyEle => filters[key].includes(keyEle));
}
return filters[key].includes(product[key]);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment