Javascript (AngularJS) 如何遍历数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25885663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:00:47  来源:igfitidea点击:

(AngularJS) how to loop through an Array

javascriptarraysangularjs

提问by lipdjo

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

那是 php.i 上的一个数组,已将其转换为 json(使用 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.

请我想知道如何在 javascript 中解析(codehttp 字段)最终的 json 变量。我想将其放入 angularjs 控制器中。

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';
        }
}

回答by Nitsan Baleli

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:

首先,您需要确保您的响应实际上被视为 JSON(从而产生正确的 javascript 对象),然后您可以使用以下三种方式之一:

Using angularjs's own method angular.forEach

使用 angularjs 自己的方法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”循环:

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.forEachmethod:

使用(相对较新的)原生 Array.prototype.forEach方法:

$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';
    }
})