Currently, I have implemented four functions that select entries from an array based on different properties.
if ($scope.filters.filter1)
$scope.filteredEntries = $scope.filteredEntries.filter(function (o)
{
return o.field1 === $scope.filters.filter1
});
if ($scope.filters.filter2)
$scope.filteredEntries = $scope.filteredEntries.filter(function (o)
{
return o.field2 === $scope.filters.filter2
});
However, this code repetition is not ideal. I am looking for a way to encapsulate the selection logic into a separate function, but since the conditions for selecting are different in each case, I am unsure how to proceed. Can I somehow pass the condition as a parameter or implement it differently? Any suggestions on how to achieve this would be appreciated.
In my approach, I envision creating something like the following pseudo code:
expression = e => e.field == value;
miniFilter(expression);
...
function miniFilter(expression)
{
$scope.filteredEntries = $scope.filteredEntries.filter(function (expression)
{
return expression;
});
}
I hope my objective is clear. My understanding of JavaScript's methodology is limited, so please bear with me. Thank you.