samplejs
samplejs
can randomly sample elements from an array as well as keys of an object (where the value of the key is the weight).
Installation
npm install samplejs
Sample Arrays
; // test has 2/5 chance of being picked// all other elements have a 1/5 chancevar array = 'test' 'another' 'test' 'more' 'element';; //=> 'test' | 'another' | 'more' | 'element' ; //=> null
Sample Weighted Objects
; // key1 has 0.5/4.5 chance of being picked// key2 has 3/4.5 chance of being picked// key3 has 1/4.5 chance of being pickedvar obj = key1: 05 key2: 3 key3: 1;; //=> 'key1' | 'key2' | 'key3' ; //=> null