getRandomValue
Using
Function selects random items from array, object, map, set. If a primitive, it just returns the primitive as it is.
Demo - use JSON
getRandomValue( )
false
Arguments
Argument | Type | Description | Example |
---|---|---|---|
input | any | Can be any type | ["Array"] { name: "Alina" } new Map() 14 |
Returns
any
— selected random value from input
or input
for primitive.
Examples
getRandomValue([1, 2, 3]);
// 2
getRandomValue([1, 2, 3]);
// 3
getRandomValue({ name: 'Alina', age: 20, sex: 'w' });
// "Alina"
getRandomValue({ name: 'Alina', age: 20, sex: 'w' });
// 20
getRandomValue(66);
// 66
getRandomValue('text');
// "text"