random-item-from
v1.0.0
Published
Get random item from strings, arrays, dictionaries and maps
Downloads
11
Maintainers
Readme
⚙️ Installation
npm i random-item-from
CDN Links:
- https://cdn.jsdelivr.net/npm/[email protected]/random-item-from.js
- https://www.unpkg.com/[email protected]/random-item-from.js
📖 Usage
● Import
// ES6
import randomItemFrom from "random-item-from";
// commonjs
const randomItemFrom = require("random-item-from");
● Random item from map
const colors = new Map([
['red', '#FF0000'],
['green', '#00FF00'],
['blue', '#0000FF']
]);
const randomColor = randomItemFrom(colors);
console.log(randomColor); // output: "#0000FF"
● Random item from array
const fruits = ['apple', 'banana', 'orange', 'pear'];
const randomFruit = randomItemFrom(fruits);
console.log(randomFruit); // output: "pear"
● Random item from string
const digits = '0123456789';
const randomDigit = randomItemFrom(digits);
console.log(randomDigit); // output: "6"
● Random item from dictionary
const pets = {
cat: 'fluffy',
dog: 'rover',
bird: 'tweety',
fish: 'goldie'
};
const randomPet = randomItemFrom(pets);
console.log(randomPet); // output: "tweety"