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.

Revisions

  1. sumitk revised this gist Jul 29, 2019. 2 changed files with 27 additions and 13 deletions.
    27 changes: 27 additions & 0 deletions MultiSelectFiltersApplication
    Original 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]);
    });
    });
    };
    13 changes: 0 additions & 13 deletions fianlMultiFilter.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    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]);
    });
    });
    };
  2. @stomg7969 stomg7969 created this gist May 14, 2019.
    13 changes: 13 additions & 0 deletions fianlMultiFilter.js
    Original 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]);
    });
    });
    };