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.
- 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
3 Answers
Reset to default 4Use 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
版权声明:本文标题:javascript - AngularJS get length of specific item in JSON array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167394a2426079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论