steffi.m steffi.m - 1 year ago 65
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.

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

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.


Answer Source

The simplest solution is to use limitTo

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