Steve Steve - 1 year ago 70
AngularJS Question

Angular JS ng-repeat choose random grouping from array?

I have an array of 20 objects.

I have a list with an ng-repeat where I want to display 3 unique items from those 20 items.

One each refresh, it might pull 3 different items, but it's ok if it does not, the only thing is that the 3 cannot have duplicates amongst themselves.

How to accomplish?

As an example, if the array is

[red, yellow, blue, green, purple, cyan, fuchsia]
then on refresh I could get:


etc. As you can see, I don't care that blue came up twice in a row there, but I must never get
red, blue, blue


Answer Source

User random and limitTo filters.

<p ng-repeat="i in list|orderBy:random|limitTo:3">{{i}}</p>