Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Which is the easiest way to loop over this array in JS?

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

Thanks in advance!

share|improve this question

In html with angular:

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

Or in js, use for-loops:

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

share|improve this answer

By using ng-repeat:

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

will yield as result 45 67 4 7.8 6.8 56 8 7 8.7

In javascript (angularjs it's not necessary here)

masterArray.forEach(function(subArray) {
   subArray.forEach(function(element) {
       console.log(element);
   }); 
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.