Vereonix
Vereonix

Reputation: 1434

angularjs filter in the controller where value isn't empty

I have a JSON object array, and I'm trying to filter the list down to only the objects where a value exists for a specific field.

The field I want to filter by can contain pretty much anything so theres nothing I can really look for to match against other than where a value exists.

I need to apply the filter in the angularjs controller.

The below works if I do a match gaainst a specific value like 'bob', but not all objects will have a value for "newName".

$scope.CorrectedNames = $filter('filter')($scope.dataList, { newName: 'bob' }).length;
console.log('Total Names to Update: '+$scope.totalCorrectedAccountNumbers.length);

I've tried

newName: '!'

this returns zero results.

newName: '!""'

this returns the entire json list

and they're my only 2 ideas I've come up with from searching.

The field value are blank and not NULL, otherwise I think the following would work.

newName: '!=null' 

Upvotes: 0

Views: 161

Answers (1)

Shantanu
Shantanu

Reputation: 3513

So you don't want to show objects with newName property value either null or nullstring. You should make use of pure js .filter method as follows:

$scope.CorrectedNames = $scope.dataList.filter(function(a) {
    if(a.newName) {
    return a.newName.trim().length !== 0;
    }
});

By doing this in $scope.CorrectedNames array you'll get all the objects from dataList excluding newName with null or nullstring or objects with no newName property.

Plunker Demo

Another way is you can create custom filter & inside its implementation you can do same above .filter() method usage for filtering.

Upvotes: 1

Related Questions