Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. /**
  2. * Filters an array of objects by custom predicates.
  3. *
  4. * @param {Array} array: the array to filter
  5. * @param {Object} filters: an object with the filter criteria
  6. * @return {Array}
  7. */
  8. function filterArray(array, filters) {
  9. const filterKeys = Object.keys(filters);
  10. return array.filter(item => {
  11. // validates all filter criteria
  12. return filterKeys.every(key => {
  13. // ignores non-function predicates
  14. if (typeof filters[key] !== 'function') return true;
  15. return filters[key](item[key]);
  16. });
  17. });
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement