user5798214
user5798214

Reputation: 559

Filter Json data in angularjs Controller

I am creating web app using angular js. I have array of json object. I need the data who have status active and pending.here is my array:

$scope.response =  staticRoutes : [
    {'id':1,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':2,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':3,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':4,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':5,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':6,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':7,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':8,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':9,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':10,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':11,'name':'ABC 1','subnet':'SUB 101','gateway':'GATE 101','status':'Active'},
    {'id':12,'name':'ABC 2','subnet':'SUB 102','gateway':'GATE 102','status':'In Active'},
    {'id':13,'name':'ABC 3','subnet':'SUB 103','gateway':'GATE 103','status':'Pending'}
]

Upvotes: 1

Views: 3544

Answers (4)

Karan
Karan

Reputation: 1118

Try this:

var filterdata = [];
        angular.forEach($scope.response,function(value,key){
            if(value.status == "active" || value.status == "pending")
            {
                filterdata.push(value);

            }   
        })
        console.log(filterFlows)

Upvotes: 2

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

For a simple filter, Underscore really isn't needed, unless you're targeting older browsers. All semi modern browsers support Array.prototype.filter(). See here for information on how to use it and browser support.

Use it like this:

 var filteredArray = $scope.response.filter(function(response){
     return response.status === 'Active' || response.status === 'Pending'
 });

Upvotes: 1

Amit Sirohiya
Amit Sirohiya

Reputation: 333

UnderscoreJs is very useful in these type of calculation

Solution -

 var filteredData = _.filter($scope.response,function(response){
      return response.status === 'Active' || response.status === 'Pending'
 });

filteredData will be an array of filtered contents.

Upvotes: 0

Ravi Tiwari
Ravi Tiwari

Reputation: 962

Assuming your array is called staticRoutes, you could do

$scope.response = staticRoutes.filter(function(item){
    return item.status.toLowerCase() === 'active' || item.status.toLowerCase() === 'pending';
});

Upvotes: 0

Related Questions