(AngularJS)如何在数组中循环
(AngularJS) how to loop through an Array
这是php上的一个数组。我已经将其转换为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
)
)
我想知道如何在javascript中解析(codehttp字段)最终的json变量。我想把它变成一个angularjs控制器。
我试过这个代码(在控制器中),但它不起作用
$scope.flag_a = 'good';
for(var key in $scope.content.codehttp)
{
if($scope.content.codehttp[key] != '200')
{
$scope.flag_a = 'bad';
}
}
首先,您需要确保您的响应实际上被视为JSON(从而产生正确的javascript对象),然后您可以使用以下三种方法之一:
使用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';
}
})
使用纯"for"循环:
for(i=0;i<$scope.content.codehttp.length;i++) {
if ($scope.content.codehttp[i] != 200) {
$scope.flag_a = 'bad';
}
}
使用(相对较新的)本机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';
}
})
相关文章:
- javascript:数组循环
- 如何将数组循环到JSON代码中
- 而与数组循环
- JavaScript 数组/循环不起作用
- AJAX在数组循环中调用,只在上一次完成后调用next
- 数组循环以获取单个值
- JavaScript数组循环
- Javascript 2D数组循环停止
- 通过Javascript中的JSON数组循环
- 为什么以下 forEach 数组循环未返回未定义
- 如何在数组循环中过滤数据
- 如何在数组循环 222 中过滤数据
- 数组循环以确定范围平均值
- 对象的数组循环
- 对象键及其值的数组循环
- 虽然的缺点!未定义的数组循环
- 数组循环导致无限循环
- 数组循环 - 处理下拉列表中的空值
- 在 Node 中连接数组循环.js glob 结果
- JavaScript 图像数组循环