random-element-selector
v1.0.2
Published
Returns one random element from the passed-in array
Downloads
8
Maintainers
Readme
Random element selector
random-element-selector
returns one randomly chosen element from the passed-in array.
Setup
Install the package:
npm install random-element-selector
Usage
import randomElement from 'random-element-selector';
const myArray = [3, 'cat', 'didgeridoo', { hello: 'World' }, 999];
console.log(randomElement(myArray)); // 'didgeridoo'
console.log(randomElement(myArray)); // 999
console.log(randomElement(myArray)); // { hello: 'World' }
console.log(randomElement(myArray)); // 999
console.log(randomElement(myArray)); // 3
console.log(randomElement(myArray)); // 3
console.log(randomElement(myArray)); // 'cat'
Note that passing-in an argument which is not an array will throw an error.