使用ng-repeat循环遍历Array系列

时间:2014-09-19 15:44:44

标签: javascript angularjs angularjs-ng-repeat

我希望遍历对象并获取内部数组的值(即区域或提供者)。但是这个值是变量而且未知。见下面的例子;

[{
    "region": [{
        "name": "EMEA"
    }, {
        "name": "US"
    }]
}, {
    "provider": [{
        "name": "provderOne"
    }, {
        "name": "providerTwo"
    }]
}]

如何获取“region”/“provider”(或者它可能是什么)的值,然后遍历内部数组以使用ng-repeat访问对象?

2 个答案:

答案 0 :(得分:1)

这是一个解决方案,它看起来很乱,但它是可行的:

<div ng-repeat="x in data">
    <div ng-repeat="(k, lst) in x">
        {{k}}
        <div ng-repeat="y in lst">
            <div ng-repeat="(key, value) in y">&nbsp;&nbsp;{{key}}: {{value}}</div>
        </div>
    </div>
</div>

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function ($scope) {
    $scope.data = [{
        "region": [{
            "name": "EMEA"
        }, {
            "name": "US"
        }]
    }, {
        "provider": [{
            "name": "provderOne"
        }, {
            "name": "providerTwo"
        }]
    }];
});

DEMO:http://jsfiddle.net/ue1fcw7q/

答案 1 :(得分:0)

你可以用这个:

<script>
 var arr = [{
    "region":[
        {"name":"EMEA"},
        {"name":"US"}
    ]
},
{
    "provider":[
        {"name":"provderOne"},
        {"name":"providerTwo"}
    ]
}];

 for(i in arr){
        var val = arr[i];
        for(j in val){
            var sub_key = j;
            console.log(sub_key);
            for(k in val[j]){
         var val1 = val[j][k];
         for(m in val1){
            console.log(val1[m]);

        }

    }
        }
 }

 </script>