Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Forked from jherax/arrayFilterFactory.1.ts
Created September 6, 2018 10:38
Show Gist options
  • Select an option

  • Save jordanrios94/d0bf97cbe1288ef90a8ed1f59ad73c22 to your computer and use it in GitHub Desktop.

Select an option

Save jordanrios94/d0bf97cbe1288ef90a8ed1f59ad73c22 to your computer and use it in GitHub Desktop.

Revisions

  1. @jherax jherax revised this gist Mar 15, 2017. 2 changed files with 22 additions and 20 deletions.
    17 changes: 8 additions & 9 deletions multiFilter.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,15 @@
    /**
    * 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
    * 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) {
    let filterKeys = Object.keys(filters);
    const 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);
    });
    // dynamically validate all filter criteria
    return filterKeys.every(key => !!~filters[key].indexOf(item[key]));
    });
    }
    }
    25 changes: 14 additions & 11 deletions test.js
    Original 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: "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]
    };

    // expected
    let expected = [
    { name: "A", color: "Blue", "size": 50 },
    { name: "C", color: "Black", "size": 70 }
    ];

    // filter the products array by color: blue and black
    // and also by size: 70 and 50
    var filtered = multiFilter(products, filters);

    console.info('Expected');
    console.log(expected);
    console.info('Filtered');
    console.log(filtered);
    console.info('Filtered:');
    console.log(filtered);

    /* expected
    [
    { name: "A", color: "Blue", "size": 50 },
    { name: "C", color: "Black", "size": 70 }
    ]
    */
  2. @jherax jherax created this gist Sep 29, 2016.
    16 changes: 16 additions & 0 deletions multiFilter.js
    Original 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);
    });
    });
    }
    23 changes: 23 additions & 0 deletions test.js
    Original 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);