Search code examples
javascriptangularjsarraysforeachangularjs-filter

Angular custom filter that returns if the array has the matching string


I've been scratching my head for awhile to get this right. I feel like I am almost there but can't get the last piece right to make it work. Here's what I am trying to do. I am creating a custom filter that will loop through my json array to see if there's "status" key and if that "status" key contains "SUCCESS" field, it will return all the object that matches that array. If the "status" key isn't there, then don't show the array object. I can see that my loop looks correct since it's figuring out which object array contains the key word but when I try to print out my results, I don't see anything. Please help!

Here's the demo.
https://jsfiddle.net/missggnyc/6r9oophu/20/

HTML

<div ng-app="mySessions">   
    <div ng-controller="sessionController">
  <table>
    <tr>
      <td>Session ID</td>
      <td>Session Name</td>

    </tr>
    <tr ng-repeat="cs in cSessions | successSessionFilter"> 
      <td>{{cs.id}}</td>
      <td>{{cs.session_name}}</td>          
    </tr>
  </table>
</div>

JS

var app = angular.module('mySessions', []);
app.controller("sessionController", ['$scope', function($scope){
        $scope.cSessions = [
    {"id":2,"session_name":"BD20","session_description":"","status":["INPROGRESS"]}, 
    {"id":3,"session_name":"CS03","session_description":"","status":["INPROGRESS", "SUCCESS"]},
    {"id":4,"session_name":"MIS99","session_description":"","status":["FAIL","NOTINPROGRESS"]},
     {"id":5,"session_name":"XGY7","session_description":"","status":["SUCCESS", "NOTINPROGRESS"]},
      {"id":6,"session_name":"DCCC2","session_description":""},
        {"id":7,"session_name":"IUOS33","session_description":""}
    ];
}]);
app.filter('successSessionFilter', function(){
        return function(cObj){          
            angular.forEach(cObj, function(value, key){
                angular.forEach(value, function(v, k){
                    //console.log('k is ' + k + ' and v is '+ v);
                    if(k === 'status'){
                        if(v.indexOf("SUCCESS") > -1){ //if it exists
                            //console.log("SUCCESS exists  "+ v.indexOf("SUCCESS"));
                            //console.log(v);
                            return true;
                        }
                        else { //it the status field isn't there, then don't show the object
                            return false;
                        }                       
                    }
                    else 
                        return false;
                })          
            })
    // console.log(cObj);
        }
    });

Solution

  • Here we go

    app.filter('successSessionFilter', function(){
      return function(items){           
        var filtered = items.filter(function(item){
          if(item['status'] && item['status'].indexOf('SUCCESS') != -1){
            return item;
          }
        })
        return filtered;
      }
    });
    

    Demo https://jsfiddle.net/ogycjbkL/