jeudi 23 avril 2020

Get random sample of items from array, where items (which are also arrays) have things totaling to X

In an array such as:

const myList = [['hi'], ['hello', 'bye'], ['ok'], ['blue', 'green', 'purple'], ['big', 'small', 'medium', 'orange', 'sky', 'ground', 'earth'], ['king', 'queen'], ['desk']]

How can I get a random sample of items in the array, but the total should be determined by input variable size.

So if the size = 3, an array would be returned such as

[['desk'], ['king', 'queen']]

or maybe

[['blue', 'green', 'purple']]

or even

[['ok'], ['desk'], ['hi']]

If size is greater than the number of items in a flattened myList, then it should just return the maximum available items.

I haven't been able to figure this out, how can this be done?




Aucun commentaire:

Enregistrer un commentaire