AngularJS循环遍历数组

时间:2016-08-29 08:02:39

标签: javascript angularjs arrays loops

在JS中循环遍历此数组的最简单方法是什么?

[[45,67,4],[7.8,6.8,56],[8,7,8.7]]

提前致谢!

2 个答案:

答案 0 :(得分:1)

带角度的html:

<!-- assuming myArray is a variable on $scope object -->
<div ng-repeat="innerArray in myArray"> 
    <div ng-repeat="value in innerArray"> 
        {{ value }}
    </div>
</div>

或者在js中,使用for - 循环:

&#13;
&#13;
var myArray = [[45,67,4],[7.8,6.8,56],[8,7,8.7]];
    
for (var i = 0; i < myArray.length; i++) {
    var innerArray = myArray[i];
    // loop through inner array
    for (var j = 0; j < innerArray.length; j++) {
        var myValue = innerArray[j];
        console.log(myValue);
    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用ng-repeat:

<div ng-repeat="subArray in masterArray"> 
   <div ng-repeat="element in subArray"> 
       {{element}}
   </div>
</div>

将产生结果45 67 4 7.8 6.8 56 8 7 8.7

在javascript(angularjs,这里没有必要)

masterArray.forEach(function(subArray) {
   subArray.forEach(function(element) {
       console.log(element);
   }); 
});