misund / weighted-random-object

Get a weighted random object from an array of objects.

Home Page:https://www.npmjs.com/package/weighted-random-object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

weighted-random-object

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.

Usage

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'

Contributing

I appreciate your issues and PRs on Github!

Testing

yarn build && yarn test

Releasing

This project uses np.

  1. Make sure your changes are in master
  2. Run yarn release
  3. Follow the interactive release guide

About

Get a weighted random object from an array of objects.

https://www.npmjs.com/package/weighted-random-object

License:GNU General Public License v3.0


Languages

Language:JavaScript 100.0%