Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Filters an array of objects by custom predicates.
- *
- * @param {Array} array: the array to filter
- * @param {Object} filters: an object with the filter criteria
- * @return {Array}
- */
- function filterArray(array, filters) {
- const filterKeys = Object.keys(filters);
- return array.filter(item => {
- // validates all filter criteria
- return filterKeys.every(key => {
- // ignores non-function predicates
- if (typeof filters[key] !== 'function') return true;
- return filters[key](item[key]);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement