admin管理员组

文章数量:1327581

Hey I have a JSON array that I'm calling from a service. I have some counters that I'd like to print out the length of the total array and length of some sub items. I'm able to print out the full length of the array but I also would like to print out the total number of items that have a progress "green", "red", and "yellow". Seeing that it's an array of objects, how would I go about to get the length of green, red, yellow?

JSON:

[
   {
     name: User1,
     progress: "green"
   },
   {
     name: User2,
     progress: "yellow"
   },
   {
     name: User3,
     progress: "green"
   },
    {
     name: User4,
     progress: "red"
   },
]

I'm storing the service called as so:

$scope.requestDetails = [];
$scope.requestDetails = data;

HTML:

<div class="total">{{requestDetails.length}}</div>
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>

Out of curiosity, I tried to print {{requestDetails.progress.length}} but it es up empty and printing {{requestDetails[0].progress.length}} prints out the number of letters of the first object's progress value.

Hey I have a JSON array that I'm calling from a service. I have some counters that I'd like to print out the length of the total array and length of some sub items. I'm able to print out the full length of the array but I also would like to print out the total number of items that have a progress "green", "red", and "yellow". Seeing that it's an array of objects, how would I go about to get the length of green, red, yellow?

JSON:

[
   {
     name: User1,
     progress: "green"
   },
   {
     name: User2,
     progress: "yellow"
   },
   {
     name: User3,
     progress: "green"
   },
    {
     name: User4,
     progress: "red"
   },
]

I'm storing the service called as so:

$scope.requestDetails = [];
$scope.requestDetails = data;

HTML:

<div class="total">{{requestDetails.length}}</div>
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>

Out of curiosity, I tried to print {{requestDetails.progress.length}} but it es up empty and printing {{requestDetails[0].progress.length}} prints out the number of letters of the first object's progress value.

Share Improve this question asked Sep 7, 2015 at 16:51 Anish S.Anish S. 531 silver badge4 bronze badges 1
  • Look at using array filter method, which you can use to create a new array with only the progress values you want, then ask for the length of that. developer.mozilla/en/docs/Web/JavaScript/Reference/… – azium Commented Sep 7, 2015 at 16:56
Add a ment  | 

3 Answers 3

Reset to default 4

Use the angular built in filter:

{{requestDetails.lenght}}

{{(requestDetails | filter: {progress: 'red'}).length}}

You can also use a custom function on the controller for custom logic:

Controller code:

  $scope.colorFilter = function(color) {
    return function (obj) {
      return obj.progress === color;
    };
  };

{{(requestDetails | filter: colorFilter('red')).length}}

Note: since Angular 1.3 filters must declare their dependencies explicitly to create a $watcher on them, otherwise those parameters will not be dirty checked within the $digest cycle, and the output will not be affected by any 2-way bindings.

Plunker: http://plnkr.co/edit/zgzSqeMVnCQbpDitrSI3?p=preview

You can prepare your data in the controller, counting every element:

$scope.itemData = {};
for (var i = 0; i < $scope.requestDetails.length; ++i) {
  var detail = $scope.requestDetails[i];
  if ($scope.itemData[detail.progress] === undefined) {
    $scope.itemData[detail.progress] = 0;
  }
  $scope.itemData[detail.progress]++;
}

and use it in your template:

<div class="red">{{ itemData.red }}</div>
<div class="green">{{ itemData.green }}</div>

If I am understanding the question correctly, the goal is to display counts of each progress type from the object?

If this is the case, here is one possible solution.

HTML

<div>{{requestDetails.length}}</div>
<div class="red">{{(requestDetails | filter: {progress: 'red'}).length}}</div>
<div class="green">{{(requestDetails | filter: {progress: 'green'}).length}}</div>

This method leverages the built in angular filter. For more reading on filters and angularjs, here are the docs.

本文标签: javascriptAngularJS get length of specific item in JSON arrayStack Overflow