Skip to content

Instantly share code, notes, and snippets.

@olibooty
Created October 9, 2023 20:41
Show Gist options
  • Select an option

  • Save olibooty/aae7ec0314d81dbd26a2bdf8dda8b047 to your computer and use it in GitHub Desktop.

Select an option

Save olibooty/aae7ec0314d81dbd26a2bdf8dda8b047 to your computer and use it in GitHub Desktop.
export const filterByPropertyKey = <TObj extends Object,>(
collection: Readonly<TObj[]>,
filters: Record<string, unknown>,
) => {
if (Object.keys(filters).length === 0) {
return collection;
}
return collection.filter((item) => {
let include = true;
for (const key in item) {
if (filters[key] === undefined) {
continue;
}
include = include && item[key] === filters[key];
}
return include;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment