🎲 Weighted Map
Select a random element from a weighted array.
The package is on the NPM registry as
weighted-map. Simply install it with your NPM client of choice.
First, import the module:
const weightedMap =
select() function takes a Map. Maps should be of type
Map<T, number> where key
T is your object, and value
number is the relative weight for that key.
There is also the
selectUnique*() ES6 generator which will return randomly but without sequential repeats.
Both return one object from the array, with the whole object intact.
// Import the moduleconst select =const map =console // -> Will return 'jerry' twice as often as 'ben'