steffi.m steffi.m - 5 months ago 11
AngularJS Question

ng-repeat on array based on length of second array

I am trying to iterate over an array using ng-repeat to display in a list. The number of records to display should be equal to the length of a second array.

<ul>
<li ng-repeat="item in firstArray"></li>
</ul>

var firstArray = ["a","b","c","d","e","f"]
var secondArray = ["x","y","z"]


The expected result for the above example :

var output = ["a","b","c"]


Since the length of secondArray is 3, the number of li elements would be 3 with the first three values from firstArray.

Should i use filter? I am using angularjs.

Thanks

Answer

The simplest solution is to use limitTo

<ul>
  <li ng-repeat="item in firstArray | limitTo:secondArray.length">{{item}}</li>
</ul>

http://plnkr.co/edit/LWh1uqNPxTy09u3MP677?p=preview