Steve Steve - 3 months ago 17
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:

red,blue,green
purple,blue,yellow
fuchsia,green,red


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
.

Do-able?

Answer

User random and limitTo filters.

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