Get a weighted random object from an array of objects with a weight property.
The objects should have a property with key 'weight' and a numerical value.
var weightedRandomObject = require("weighted-random-object");
var fruitBasket = [
{
"type": "apple",
"weight": 33
}, {
"type": "pear",
"weight": 66
}
];
var fruit = weightedRandomObject(fruitBasket);
console.log(fruit.type); // sometimes 'apple', more often 'pear'
I appreciate your issues and PRs on Github!
yarn build && yarn test
This project uses np.
- Make sure your changes are in master
- Run
yarn release
- Follow the interactive release guide