To get a sample of 5 random elements from an array in JavaScript, you can use the following code:
index.tsx136 chars6 lines
The sort()
method without any arguments is used here to shuffle the array randomly. Then the slice()
method is used to select the first 5 elements from the shuffled array. Finally, the resulting sample is logged to the console.
gistlibby LogSnag