Skip to content

Instantly share code, notes, and snippets.

@c01nd01r
Last active April 8, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c01nd01r/afa4ef3fc4676acdf6c7d20143deb650 to your computer and use it in GitHub Desktop.
Save c01nd01r/afa4ef3fc4676acdf6c7d20143deb650 to your computer and use it in GitHub Desktop.
multiple array filters with functions
const data = [
{id: 1, total: 9, type: 'cashout'},
{id: 2, total: 8, type: 'cashin'},
{id: 3, total: 5, type: 'cashout'},
{id: 4, total: 4, type: 'cachin'},
{id: 5, total: 3, type: 'cashout'},
];
const filters = {
total(val) {
return val.total > 4;
},
type(val) {
return val.type === 'cashout';
},
};
const filteredData = data.filter( item =>
Object.keys(filters).every(filterIdx => filters[filterIdx](item))
);
console.log('result:', filteredData)
// result: Array(2)
// [
// {id:1, total: 9, type: 'cashout'},
// {id:3, total: 5, type: 'cashout'},
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment