angular iterate over json

[亡魂溺海] 提交于 2019-12-29 07:43:32

问题


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"; 

here is my html

<div ng-controller="MyCtrl">
<div ng-repeat="user in _users | filter:isActive">
    {{user.User.userid}}
</div>
</div>

here is my 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"}
                ]}
            ]
        }}];
}

here is a demo link http://jsfiddle.net/4kzzy/174/


回答1:


Nothing complicated, it’s just that your syntax is wrong.

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.

See http://jsfiddle.net/4kzzy/176/

Also note you could use angular.forEach instead.




回答2:


I'm not sure whether it is suitable solution for your problem, but if you want to display only active users, try this design:

<div ng-controller="MyCtrl">
<div ng-repeat="user in _users | filter:{User.active:1}">
        {{user.User.userid}}
</div>
</div>

Show: 19571

http://jsfiddle.net/Jd2Hw/




回答3:


Well, it's pretty obvious that it won't work, as there is no variable called data in the user.User.Stats path. It seems like there is a part of your business logic missing. When users should be filtered out? When they have no Stats, or all cells in Stats array have records with active === 1 ? To see all the cells in Stats array the proper syntax would be:

for(var i=0; i < user.User.Stats.length; $i++;){
    var singleCell = user.User.Stats[i];
    // singleCell.active === "1";    
}


来源:https://stackoverflow.com/questions/20987604/angular-iterate-over-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!