Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular iterate over json

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/

like image 714
Asim Zaidi Avatar asked Jan 08 '14 05:01

Asim Zaidi


People also ask

Can you loop through a JSON Object?

There may be times where you feel you need to make a loop through the array of JSON objects in JavaScript. However, you no longer need to worry about this, as it can be done using an array of numbers, strings, or objects.

How to iterate over JSON Object in JS?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

How do I add a list to a JSON object?

How do I add a list to a JSON object? JSONObject obj = new JSONObject(); List sList = new ArrayList (); sList. add(“val1”); sList. add(“val2”); obj.

How do I iterate a JSON array in node JS?

To iterate JSON array, use the JSON. parse().


1 Answers

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.

like image 98
Mormegil Avatar answered Sep 28 '22 14:09

Mormegil