A lightweight filter util to make client-side data filtering a breeze.
npm i --save filter-by
import { filterBy } from 'filter-by';
const data = [{ age: 21, name: "John Doe" }, { age: 40, name: "Jane Doe" }];
const filters = [{ field: "age", type: "EQ", value: 21 }];
// [{ age: 21, name: "John Doe" }];
filterBy(data, filters);
The following filter types are available
{ field: "age", type: "EQ", value: 21 }
{ field: "age", type: "NEQ", value: 21 }
{ field: "age", type: "LT", value: 21 }
{ field: "age", type: "LTE", value: 21 }
{ field: "age", type: "GT", value: 21 }
{ field: "age", type: "GTE", value: 21 }
{ field: "age", type: "RANGE", value: [20, 30] }
{ field: "name", type: "LIKE", value: "%value%" }
{ field: "name", type: "LIKE", value: "%value%" }
We can check if any of the specified values can be found inside the data array.
{ field: "name", type: "LIKE", value: ["string1", "string2", "%string3%"] }
Works with every filter type.
We can check if any of the specified values can be found across different fields in the data array.
{ field: ["firstName", "lastName'], type: "ILIKE", value: "hudson" }
Works with every filter type.
There is a helper function to create a filter for every filter type.
We have to wrap the filter utils inside createFilters<DataType>
for TypeScript support and field type inference.
import { filterBy, createFilters, eq, neq, gte } from 'filter-by';
const filters = createFilters<Data>([
eq("age", [23, 21]),
neq("name", "John"),
gte("score", 100),
]);
filterBy(data, filters)