Skip to content

Instantly share code, notes, and snippets.

@bryanltobing
Created March 19, 2021 08:53
Show Gist options
  • Save bryanltobing/c5d527ad0fcd9df0eaa7e0bf05f78c49 to your computer and use it in GitHub Desktop.
Save bryanltobing/c5d527ad0fcd9df0eaa7e0bf05f78c49 to your computer and use it in GitHub Desktop.
const filterFunction = (list, options) => {
switch (options.option) {
case 'age':
return filerByAgeRange(list, options.payload);
case 'name':
return filerByNames(list, options.payload);
case 'location':
return filterByLocation(list, options.payload);
}
};
const filerByAgeRange = (list, payload) => {
return list.filter((data) => {
return data.age > payload.min && data.age < payload.max;
});
};
const filerByNames = (list, payload) => {
return list.filter((data) => {
return data.name.startsWith(payload);
});
};
const filterByLocation = (list, payload) => {
return list.filter((data) => {
return data.location === payload;
});
};
module.exports = filterFunction;
/* ======= How to use ======= */
// Call the filterFunction from your main file
// filter(list, options)
// * List of an array of object
// * Options, option and payload
/* ======= Example ======= */
// 1. Filter by age range
// filterFunction(personsLkist, { option: 'age', payload: { min: 16, max: 19 } })
// 2. Filter by startName
// filterFunction(persons, { option: 'name', payload: 'D' })
// 3. Filter by location information
// filterFunction(persons, { option: 'location', payload: 'california' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment