A damn small library for filtering data based upon multiple parameters in a "or" or "and" fashion.
- Have an
array
ofobjects
that you wish to filter upon
var students = [
{
name: ['Suzan'],
classes: ['Biology', 'Mathematics', 'Music', 'Programming'],
graduated: [false]
},
{
name: ['Michael'],
classes: ['Music', 'Movies', 'Drama'],
graduated: [false]
},
{
name: ['Douglas'],
classes: ['Mathematics', 'Drama'],
graduated: [true]
},
{
name: ['Jessica'],
classes: ['Programming'],
graduated: [true]
}
]
- Create a
query object
witharrays
of desired results- Using the or operator (default if no operator specified)
var studentQuery = {
classes: ['Programming', 'Drama'],
graduated: [true],
operator: '||'
}
// Filter on students with classes 'Programming' or 'Drama' and 'graduated: true'
- Using the and operator
var studentQuery = {
classes: ['Programming', 'Drama'],
graduated: [true],
operator: '&&'
}
// Filter on students with classes 'Programming' and 'Drama' and 'graduated: true'
- Initiate the filter by creating an instance of it and provide the array and query
var filteredStudents = new SmallFilter({
query: studentQuery,
items: students
})
- Get the filter results
filteredStudents.filter();
- Results in
- || operator
// Returns students Douglas and Jessica (as they match some of the queried classes and have graduated)
- && operator
// Returns student Douglas (as he is the only one matching all the queried classes and have graduated)