最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (AngularJS) how to loop through an Array - Stack Overflow

programmeradmin0浏览0评论

That is an array on php.i have transformed it in json (with json_encode)

Array
(
    [codehttp] => Array
        (
            [0] => 200
            [1] => 200
            [2] => 200
            [3] => 200
        )

    [time] => Array
        (
            [0] => 2014-09-15 13:54:04
            [1] => 2014-09-15 13:54:04
            [2] => 2014-09-15 13:54:04
            [3] => 2014-09-15 13:54:04
        )

    [channel] => Array
        (
            [0] => channel1
            [1] => channel1
            [2] => channel1
            [3] => channel1
            [4] => channel1
        )

    [type] => Array
        (
            [0] => android
            [1] => android
            [2] => android
            [3] => android
            [4] => android
        )

    [indice] => Array
        (
            [0] => masterplaylist
            [1] => video_110000
            [2] => video_190000
            [3] => video_300000
            [4] => video_500000
        )

    [cdn] => Array
        (
            [0] => cdn1
            [1] => cdn1
            [2] => cdn1
            [3] => cdn1
            [4] => cdn1
        )

)

Please I would like to know how to parse (the codehttp field) in javascript the final json variable. I would like to do it into an angularjs controller.

I have tried this code (in a controller) but it does'nt work

$scope.flag_a = 'good';

for(var key in $scope.content.codehttp)
{
        if($scope.content.codehttp[key] != '200')
        {
                $scope.flag_a = 'bad';
        }
}

That is an array on php.i have transformed it in json (with json_encode)

Array
(
    [codehttp] => Array
        (
            [0] => 200
            [1] => 200
            [2] => 200
            [3] => 200
        )

    [time] => Array
        (
            [0] => 2014-09-15 13:54:04
            [1] => 2014-09-15 13:54:04
            [2] => 2014-09-15 13:54:04
            [3] => 2014-09-15 13:54:04
        )

    [channel] => Array
        (
            [0] => channel1
            [1] => channel1
            [2] => channel1
            [3] => channel1
            [4] => channel1
        )

    [type] => Array
        (
            [0] => android
            [1] => android
            [2] => android
            [3] => android
            [4] => android
        )

    [indice] => Array
        (
            [0] => masterplaylist
            [1] => video_110000
            [2] => video_190000
            [3] => video_300000
            [4] => video_500000
        )

    [cdn] => Array
        (
            [0] => cdn1
            [1] => cdn1
            [2] => cdn1
            [3] => cdn1
            [4] => cdn1
        )

)

Please I would like to know how to parse (the codehttp field) in javascript the final json variable. I would like to do it into an angularjs controller.

I have tried this code (in a controller) but it does'nt work

$scope.flag_a = 'good';

for(var key in $scope.content.codehttp)
{
        if($scope.content.codehttp[key] != '200')
        {
                $scope.flag_a = 'bad';
        }
}
Share Improve this question edited Sep 17, 2014 at 9:10 Nitsan Baleli 5,4583 gold badges34 silver badges53 bronze badges asked Sep 17, 2014 at 8:25 lipdjolipdjo 1611 gold badge4 silver badges11 bronze badges 1
  • 4 for...in loops are meant for objects, not for arrays, use for(i=0;i<$scope.content.codehttp.length;++i) for an array – Elias Van Ootegem Commented Sep 17, 2014 at 8:28
Add a comment  | 

1 Answer 1

Reset to default 12

At first you need to make sure, your response actually is treated as JSON (thus results in a proper javascript object), and then you can use on of the following three ways:

Using angularjs's own method angular.forEach

$scope.content = {};
$scope.content.codehttp = [200, 200, 200, 201];

angular.forEach($scope.content.codehttp, function(value, key) {
  if (value != 200) {
    $scope.flag_a = 'bad';
  }
})

Using a plain 'for' loop:

for(i=0;i<$scope.content.codehttp.length;i++) { 
  if ($scope.content.codehttp[i] != 200) {
    $scope.flag_a = 'bad';
  }
}

Using the (relatively new) native Array.prototype.forEach method:

$scope = {};
$scope.content = {};
$scope.content.codehttp = [200, 200, 200, 201];

$scope.content.codehttp.forEach(function(value, key) {
    if (value != 200) {
        // for demonstrational purposes only:
        document.write("Entry #"+(key+1)+" contained a bad status: "+value);
        $scope.flag_a = 'bad';
    }
})

发布评论

评论列表(0)

  1. 暂无评论