0

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!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is three any option in angularJs for not to create extra one div <div ng-repeat="value in innerArray"> during iteration
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.