Created
March 11, 2022 10:31
-
-
Save AitorAlejandro/869a3f0b8003f6ba8c49793d0ce404f9 to your computer and use it in GitHub Desktop.
Get a random item of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const randomItem = <T>(arr: T[]): T => arr[(Math.random() * arr.length) | 0]; | |
randomItem(<number[]>[1, 2, 3, 4, 5]); // -> 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment