Search code examples
javascriptangularjsangularjs-filter

AngularJS filter fuzzy search


I have a filter in my controller like this:

$scope.LegEntQueue = $filter('filter')(legEncoderQueue, {FileAppCD: 'ENT'}).length;

Where I am counting the results of an array called legEncoderQueue based on a property called FileAppCD if it has the value of ENT

This works perfectly, but is there a way to have it fuzzy search and select anything that has ENT in the value? Such as ENT-M-And and ENT-M-iOS?


Solution

  • Thank you all for your input, it took me a moment to realize my ignorance, thinking I could just use some built in angular filter to do what just plain javascript could do.

    My Solution, was to create a function:

    $scope.regexCount = function(array, feild, pattern) {
        var count = 0;
        for (index = 0; index < array.length; index++) {
            if (pattern.test(array[index][feild])) {
                count ++;
            }
        }
        return count;
    };
    

    And then pass in my parameters.

    $scope.LegEntQueue = $scope.regexCount(legEncoderQueue, 'FileAppCD', /ENT.*/);