0

Say I had

 var imgs = ["pItem1","pItem2","pItem3","pItem4","pItem5"]

How can get a new array from the current one, that randomly picks lets say 3 items from the old array and puts it in a new array.

var newArray =["pItem1," "pItem4," "pItem2"];
Edi G.
  • 2,432
  • 7
  • 24
  • 33

1 Answers1

1

You're basically looking to take a randomly sampled subset of an array. One approach is to randomly shuffle the array, then take a slice from the beginning of the array. See this implementation of getRandomSubarray() in another answer: https://stackoverflow.com/a/11935263/2943575

Community
  • 1
  • 1
Joe Binney
  • 61
  • 2