Sample Array

Get N random items from an array without replacement.

Code

General
[...arr].sort(() => Math.random() - 0.5).slice(0, n);

Parameters

Array

Number of items to sample

Browser·fetch() may be limited by CORS

More JavaScript Snippets