Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda