Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How to get a random element from an array of objects using probabilities in vuejs

I try to call multiple times a method that draw an element from an array using probabilities.

My first method (first_function) is a promise that when resolved return this array :

[
  [
    {
      cardName: "Energy Fire",
      cardRatio: 50,
    },
    {
      cardName: "Energy Water",
      cardRatio: 50,
    },
  ],
  [
    {
      cardName: "Charizard",
      cardRatio: 10,
    },
    {
      cardName: "Pikachu",
      cardRatio: 30,
    },
    {
      cardName: "Rayquaza",
      cardRatio: 60,
    },
  ],
];

When the promise is resolved, i call another method (second_function) :

my_promise.then((result) => {
  for (const position in result) {
    this.second_function(result[position]);
  }
});

There is my second method (second_function) :

function second_function(pull) {
  var winner = Math.random() * 100;
  var threshold = 0;
  for (let i = 0; i < pull.length; i++) {
    threshold += pull[i].cardRatio;
    if (threshold > winner) {
      return console.log("winner", pull[i]);
    }
  }
}

I want to draw a random card from each array (this is why i call multiple times second_function in the first_function) but the problem is just one draw is resolved (i see just one console.log). However, second_function is called multiple times like expected but it looks like the for loop in the second_function is called one time only.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda