在JSON角迭代 [英] angular iterate over json
问题描述
所以我有一个JSON,我试图让只有活跃用户所有的统计信息。当我尝试在for循环中是这样做的。
So I have a json and I am trying to get all the stats for active users only. when I try to do in a for loop something like this
for(var i=0; i < user.User.Stats.data.length; $i++;){
return user.User.Stats[i].active === "1";
}
这是行不通的。不过正常工作没有循环,只要我得到只有一条记录
it doesn't work ...however works fine without for loop as long as I am getting only one record
return user.User.Stats[i].active === "1";
这是我的HTML
here is my html
<div ng-controller="MyCtrl">
<div ng-repeat="user in _users | filter:isActive">
{{user.User.userid}}
</div>
</div>
这是我的js
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.isActive = function(user) {
// for(var i=0; i < user.User.Stats.data.length; $i++;){
return user.User.Stats[0].active === "1";
// }
};
$scope._users = [
{
"User": {
"userid": "19571",
"status": "7",
"active": "1",
"lastlogin": "1339759025307",
"Stats": [
{
"active": "1",
"catid": "10918",
"typeid": "71",
"Credits": [
{
"content": "917,65",
"active": "1",
"type": "C7"},
{
"content": "125,65",
"active": "1",
"type": "B2"}
]},
{
"active": "1",
"catid": "10918",
"typeid": "71",
"Credits": [
{
"content": "917,65",
"active": "1",
"type": "C7"},
{
"content": "125,65",
"active": "1",
"type": "B2"}
]}
]
}}];
}
下面是一个演示链接 http://jsfiddle.net/4kzzy/174/
推荐答案
没有什么复杂的,它只是你的语法是错误的。
Nothing complicated, it’s just that your syntax is wrong.
for循环需要这样写:
The for loop needs to be written like this:
for(var i=0; i < user.User.Stats.length; i++)
即。没有多余的 $
,没有多余的;
,也没有数据
在统计信息
。
I.e. without the superfluous $
, without the superfluous ;
, and also there is no data
inside Stats
.
请参阅 http://jsfiddle.net/4kzzy/176/
另外请注意,你可以使用 angular.forEach
来代替。
Also note you could use angular.forEach
instead.
这篇关于在JSON角迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!