角度迭代json [英] angular iterate over json

查看:26
本文介绍了角度迭代json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 json 并且我正在尝试仅获取活动用户的所有统计信息.当我尝试在 for 循环中做这样的事情

for(var i=0; i 

它不起作用......但是只要我只得到一条记录,就可以在没有 for 循环的情况下正常工作

return user.User.Stats[i].active === "1";

这是我的 html

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

这是我的js

var myApp = angular.module('myApp', []);函数 MyCtrl($scope) {$scope.isActive = 函数(用户){//for(var i=0; i < user.User.Stats.data.length; $i++;){返回 user.User.Stats[0].active === "1";//}};$scope._users = [{用户":{"userid": "19571","状态": "7",活动":1","lastlogin": "1339759025307",统计":[{活动":1","catid": "10918","typeid": "71",学分":[{"内容": "917,65",活动":1","类型": "C7"},{内容":125,65",活动":1",类型":B2"}]},{活动":1","catid": "10918","typeid": "71",学分":[{"内容": "917,65",活动":1","类型": "C7"},{内容":125,65",活动":1",类型":B2"}]}]}}];}

这是一个演示链接 http://jsfiddle.net/4kzzy/174/

解决方案

没什么复杂的,就是你的语法不对.

for 循环需要这样写:

for(var i=0; i 

即没有多余的$,没有多余的;,也没有Stats里面的data.

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

另请注意,您可以改用 angular.forEach.

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/

解决方案

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.

这篇关于角度迭代json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆