1

I have one JSON array...

{
  "Name" : "ABC",
  "rating": [
    {
      "id": null,
      "Percentage": 40
    },
    {
      "id": 0,
      "Percentage": 40
    },
    {
      "id": 1,
      "Percentage": 20
    }
  ],
  "email" : "abc@abc.com"
}

And i want to get only percentage with id 0 and 1 not null(skip)... I am displaying this array in html with ng-repeat..., and i want to display only percentages with id is equal to 0 and 1 not null (skip).

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135

7 Answers7

1

This should be the ng-repeat for the array structure:

<div
  ng-repeat="item in items"
  ng-show="item.id != null && item.id == 0 || item.id == 1">
</div>

This is the array only, not the json object, you'll have to loop through that too prior to this.

rrd
  • 5,789
  • 3
  • 28
  • 36
1

If you only want to have those in the HTML, which 0 or 1 in the HTML, you can use the following code snippet:

HTML:

<div ng-repeat="rating in object.rating | filter: skipNull"> 

Angular Controller:

$scope.skipNull = function(item) {
    return item.id === 0 || item.id === 1;
}

Here is a JSFiddle.


You are probably better off, if you are using a function like this, which only checks for null and undefined:

$scope.skipNull = function(item) {
    return (typeof item.id !== "undefined" && item.id !== null);
}
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
0

You can use a custom filter. Like in this answer.

angular.module('app', []).
controller('ctrl', function($scope) {
  $scope.data = {
    "Name" : "ABC",
    "rating": [
      {
        "id": null,
        "Percentage": 40
      },
      {
        "id": 0,
        "Percentage": 40
      },
      {
        "id": 1,
        "Percentage": 20
      }
    ],
    "email" : "abc@abc.com"
  };
  
  $scope.noNull = function(item) {
    return item.id != null;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="item in data.rating | filter:noNull" ng-bind="item.Percentage"></li>
    </ul>
</div>
Community
  • 1
  • 1
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
0

You have to use ng-if directive. All you have to do is to apply it:

ng-if="item.id!=null"

function TodoCtrl($scope) {
  $scope.data={ "Name" : "ABC", "rating": [ { "id": null, "Percentage": 40 }, { "id": 0, "Percentage": 40 }, { "id": 1, "Percentage": 20 } ], "email" : "abc@abc.com" };
}
td{
  border:1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<div ng-app>
  <div ng-controller="TodoCtrl">
     <table>
       <tr>
         <th>Id</th>
         <th>Percentage</th>
       </tr>
       <tr ng-repeat="item in data.rating" ng-if="item.id!=null">
         <td>{{item.id}}</td>
         <td>{{item.Percentage}}</td>
       </tr>
     </table>
  </div>
</div>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $http) {
    
         
          $scope.results =  [{
   "Name": "ABC",
   "rating": [{
     "id": null,
     "Percentage": 40
   }, {
     "id": 0,
     "Percentage": 40
   }, {
     "id": 1,
     "Percentage": 20
   }],
   "email": "abc@abc.com"
 }] ;
          $scope.resultstobeDisplayed = [];
          angular.forEach($scope.results[0].rating, function(val) {
                  if (val.id != null) {
                    $scope.resultstobeDisplayed.push(val);
                  }
                 
              });
         
      });
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script src="https://code.angularjs.org/1.6.1/angular.js"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div ng-repeat="vale in resultstobeDisplayed">
   <h1>{{vale}}</h1>
  </div>


</body>

</html>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can create your filter function into the controller like this:

Controller:

$scope.hideNullRatings = function(item) {
   return item.id ===0 || item.id === 1;
}

HTML:

ng-repeat='item in items | filter: hideNullRatings'
Marco Moretti
  • 689
  • 4
  • 11
0

You can use ng-if

<div ng-repeat="rating in object.rating" ng-if="rating.id != null">
nishant agrawal
  • 471
  • 2
  • 7