Angular filter fuzzy search

Asked by At

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?

2 Answers

0
Greyhammer On Best Solutions

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.*/);

2
John F. On

There's a property $ that will perform a substring search of all your properties within your collection. In your case, you can use

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

From the angular docs

A special property name ($ by default) can be used (e.g. as in {$: "text"}) to accept a match against any property of the object or its nested object properties.