Angular js Array within another array

Asked

Viewed 6,169 times

4

How do I print an array within another array, in the.log console it comes like this:

 Array[5]
     0:Array[10]
      0:Object
      1:Object
      2:Object
      3:Object
      4:Object
      5:Object
      6:Object
      7:Object
      8:Object
      9:Object
      10:Object
    1:Array[2]
      0:Object
      1:Object

I’m trying with normal repeat and it won’t, I don’t know how I’ll be able to do it...

  • 1

    is with angular what you need?

  • 2

    Enter the code of your ng-repeat. You’ll have to do 2 to go around.

4 answers

5

Need to make a ng-repeat within another ng-repeat, to access each array and print the values:

Html:

<div ng-app="app">
  <div ng-controller="ctrl">
    <div ng-repeat="array in arrays">
      <div ng-repeat="v in array">      
         {{v.name}}
      </div>
    </div>
  </div>
</div>

Angular:

var app = angular.module('app', []);

    app.controller('ctrl', ['$scope', function($scope)
    {           
       $scope.arrays = [
        [{'name':'a1'}, {'name':'a2'}],
        [{'name':'a3'}, {'name':'a4'}]
       ];
    }]);

The arrays would be the main and array each internal item.


Example:

var app = angular.module('app', []);

    app.controller('ctrl', ['$scope', function($scope)
    {          
       $scope.arrays = [
       	[{'name':'a1'}, {'name':'a2'}],
        [{'name':'a3'}, {'name':'a4'}]
       ];
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div ng-app="app">
  <div ng-controller="ctrl">
    <div ng-repeat="array in arrays">
      <div ng-repeat="v in array">      
         {{v.name}}
      </div>
    </div>
  </div>
</div>

References:

5

Do:

Controller

$scope.meuArray = [];

//meu primeiro array é esse seu array que tem outros arrays dentro

angular.forEach(primeiroArray, function(arrays) {
    $scope.meuArray.push(arrays)
}

HTML

<div ng-repeat="obj in meuArray">
     {{obj.algumAtributo}}
<div>

3


Try to use one ng-repeat inside another:

<div ng-repeat="item in array">
 <div ng-repeat="object in item">
   {{object.name}}
 </div>
</div>
  • Practical and concise response.

0

As mentioned above, use one repeat inside another. Or you can also access directly as follows:

item[0][0] //Acessar o campo "0" do array "0"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.