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

120
Vistas
How to store objects created in a loop inside array defined outside of loop in Javascript

I have the following for loop in which this_block gets created and then filled in:

for (i = learning_trial_idx.length - 1; i >= 0; i--) {

  target = Math.floor(Math.random())

  these_trials = learning_trial_idx.filter(x => x.from != target)

  this_block = [];

  for (let i = 0; i < these_trials.length; i++) {
      this_block.push(these_trials[i])
  }

  this_block = this_block.filter(function (a) {
    var key = a.to + '|' + a.from;
    if (!this[key]) {
        this[key] = true;
        return true;
      }
    }, Object.create(null));
}

How can I go about storing this_block each time into something like const all_blocks = {}, which is defined outside of the for loop?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do that by declaring first const allBlocks = { thisBlock: [] } and push-ing inside thisBlock inside the for loop: allBlocks.thisBlock.push(theseTrials[i])

about 4 years ago · Juan Pablo Isaza Denunciar
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