1

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'}
]
1

4 Answers 4

2

Try this:

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

            }   
        })
        console.log(filterFlows)
Sign up to request clarification or add additional context in comments.

Comments

1

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'
 });

5 Comments

if i do this with underscoreJs is it wrong? I have used underscoreJS i my application and i found it very useful.
No definitely not wrong! However for this simple task, you don't need it explicitly. It's an added dependency that's mostly redundant for this case. If you're already using Underscore, because you need it for different cases, by all means go ahead. But I wouldn't add a dependency to something when it can be done just as easily without :)
yeah thanks for explanation @luka i have big application thats why i am using it. It reduces the plenty of code.
It shows me empty array. when i console.log(filteredArray )
Then maybe ` $scope.response` isn't filled yet? I see you also have staticRoutes as a name in there, you could try replacing $scope.response with staticRoutes. It's not quite clear from your code.
0

Assuming your array is called staticRoutes, you could do

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

Comments

0

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.

1 Comment

This won't work, because response isn't defined as a parameter of your anonymous function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.