Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index output if weights are provided #4

Open
azz0r opened this issue Oct 16, 2019 · 4 comments
Open

index output if weights are provided #4

azz0r opened this issue Oct 16, 2019 · 4 comments

Comments

@azz0r
Copy link

azz0r commented Oct 16, 2019

I think it would be great if the select function returns the index of the item it chose.

In the code below I would have to add another loop to find the item with that % change of winning and map it back.

const getPercentageAmount = (total, points) => points/total * 100
function App() {
  let items = [
    {
      id: 1,
      points: 89
    },
    {
      id: 1,
      points: 88
    }
  ];
  const total = items.reduce((prev, next) => prev + next.points, 0);
  const weights = []
  items = items.map(item => {
    weights.push(getPercentageAmount(total, item.points))
    return item;
  });

  items = weighted.select(weights)
// returns 50.2 or 49.2
@azz0r
Copy link
Author

azz0r commented Dec 18, 2020

@Schoonology any chance of a response on this?

@Schoonology
Copy link
Owner

The interface could be extended, but that would be a backwards-incompatible change. I'd like to modernize the internals at the same time, so that's unlikely to happen right now.

What I've done in the past to solve similar issues is to use the index as the value in the array, so that weighted.select returns the index directly, and you access a secondary array afterward. In your case, you're already calling items.map, and could extend that closure to grab the index during iteration; there's no need to hand-author a second array.

@Schoonology
Copy link
Owner

@azz0r How did that suggestion work out?

@azz0r
Copy link
Author

azz0r commented Jan 24, 2021

Hello I am using the same work around you outlined :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants