-
-
Save sumitk/f15d0d39e77f6f6addc6252bdbebaa7e to your computer and use it in GitHub Desktop.
Revisions
-
sumitk revised this gist
Jul 29, 2019 . 2 changed files with 27 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ 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]); }); }); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +0,0 @@ -
stomg7969 created this gist
May 14, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ 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]); }); }); };