README
smart-array-filter
Filter an array of objects
Installation
npm install smart-array-filter
Documentation
Example
const array = [
{ id: 'A', pet: 'dog' },
{ id: 'B', pet: 'cat' },
];
const filteredData = filter(array, {
limit: 1,
keywords: 'Do', // search for any field that contains the "Do" string
caseSensitive: true,
});
var array = [
{ id: '1', pet: 'dog' },
{ id: '2', pet: 'cat' },
{ id: '3', pet: 'horse' },
];
filter(array, {
keywords: '-pet:cat', // not a cat
});
filter(array, {
keywords: '2', // any field has the value 2
});
filter(array, {
keywords: '>2', // any field has the value after 2. Strings will be included and letters are after numbers in ascii code
});
filter(array, {
keywords: 'id:>=2', // id greater or equal to 2
});
filter(array, {
keywords: 'id:1..2', // id between 1 and 2 (including 1 and 2)
});
If you enter many criteria by default there is a 'AND' combination
var array = [{ animals: ['dog', 'cat'] }, { animals: ['horse', 'cat'] }];
filter(array, {
keywords: 'animals:cat', // array must contain a cat
});
filter(array, {
keywords: 'animals:o', // by default it is include so match dog and horse
});
filter(array, {
keywords: 'animals:=o', // must contain exactly a 'o'
});
var array = [{ a: [{ b: 1 }, { c: 2 }] }, { a: [{ b: 2 }, { d: 2 }] }];
filter(array, {
keywords: 'a.b:>=1', // 2 consecutive properties (not including array indices)
});
filter(array, {
keywords: 'dog:>=1',
pathAlias: { dog: 'a.b' }, // you can define path alias
});
filter(array, {
keywords: 'b:>1', // 2 consecutive properties (not including array indices)
});
filter(array, {
keywords: 'd:2', // 2 consecutive properties (not including array indices)
});