The goal is to be able based on input values in n HTML form to easily filter an array of objects.
The field names should have as names the key followed by an underscore and by one of the following possibilities:
- equals (or nothing): if the value must be an exact match (number or string)
- min: if the value is a number and must be >= to the value
- max: if the value is a number and must be <= to the value
- includes: if the value is a string and should include the value
$ npm i filter-array-of-object
import filter from 'filter-array-of-object';
let array = [
{ id: 1, bp: 100, mp: 0, name: 'water' },
{ id: 2, bp: 65, mp: -98, name: 'methanol' },
{ id: 3, bp: 78, mp: -114, name: 'ethanol' },
{ id: 4, bp: 97, mp: -126, name: 'n-propanol' },
{ id: 5, bp: 118, mp: -90, name: 'n-butanol' },
];
const results = filter(array, {
bp_min: 60,
bp_max: 100,
name_includes: 'eth',
});