-
-
Save jordanrios94/d0bf97cbe1288ef90a8ed1f59ad73c22 to your computer and use it in GitHub Desktop.
Revisions
-
jherax revised this gist
Mar 15, 2017 . 2 changed files with 22 additions and 20 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 @@ -1,16 +1,15 @@ /** * Filters an array of objects with multiple criteria. * * @param {Array} array: the array to filter * @param {Object} filters: an object with the filter criteria as the property names * @return {Array} */ function multiFilter(array, filters) { const filterKeys = Object.keys(filters); // filters all elements passing the criteria return array.filter((item) => { // dynamically validate all filter criteria return filterKeys.every(key => !!~filters[key].indexOf(item[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,23 +1,26 @@ let products = [ { name: "A", color: "Blue", size: 50 }, { name: "B", color: "Blue", size: 60 }, { name: "C", color: "Black", size: 70 }, { name: "D", color: "Green", size: 50 }, ]; // the value of each key is an array with the values to filter let filters = { color: ["Blue", "Black"], size: [70, 50] }; // filter the products array by color: blue and black // and also by size: 70 and 50 var filtered = multiFilter(products, filters); console.info('Filtered:'); console.log(filtered); /* expected [ { name: "A", color: "Blue", "size": 50 }, { name: "C", color: "Black", "size": 70 } ] */ -
jherax created this gist
Sep 29, 2016 .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,16 @@ /** * Multi-filter an array of objects * @param {Array} array : list of elements to apply a multiple criteria filter * @param {Object} filters: Contains multiple criteria filters by the property names of the objects to filter * @return {Array} */ function multiFilter(array, filters) { let filterKeys = Object.keys(filters); // filters all elements passing the criteria return array.filter((item) => { // validates all filters criteria dynamically return filterKeys.every((key) => { return (filters[key].indexOf(item[key]) !== -1); }); }); } 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,23 @@ let products = [ { name: "A", color: "Blue", size: 50 }, { name: "B", color: "Blue", size: 60 }, { name: "C", color: "Black", size: 70 } ]; let filters = { color: ["Blue", "Black"], size: [70, 50] }; // expected let expected = [ { name: "A", color: "Blue", "size": 50 }, { name: "C", color: "Black", "size": 70 } ]; var filtered = multiFilter(products, filters); console.info('Expected'); console.log(expected); console.info('Filtered'); console.log(filtered);