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

194
Vistas
How can I do the equivalent of "unzipping" arrays in Javascript?

I've been searching for a way to do the equivalent of zipping and unzipping arrays in Javascript. I've found a way to to the zipping, but not the unzipping.

Overview:

I have two lists and the first members of the lists go together, the second members go together, etc. I would like to shuffle the lists to retain this pairing, and then end up with two separate lists once again.

In Python:

#Define shuffle function
shuffle = util.shuffle;

#Lists of adult and child animals
adult = ["cat", "dog", "chicken"]
child = ["kitty", "puppy", "chick"]

#zip them so I can shuffle while keeping pairs intact; then unzip them
animals = list(zip(adult, child))
random.shuffle(animals)
adult, child = zip(*animals)
adult = list(adult)
child = list(child)

In Javascript:

In the PsychoPy forums I found a way to do the zipping that will work in Pavlovia.

animals = []
for Idx in range(len(adult)):
     animals.append([adult[Idx],labelsWhole[Idx%len(child)]])
shuffle(animals)

Outstanding:

But now how can I do the equivalent of: adult, child = zip(*animals)?

I am aware of this similar post. However, there is only one comment mentioning "unzipping" and it won't work in PsychoPy.

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

0

You can iterate over the zipped list and use tuple unpacking

for(const [adult,child] of zippedList)
  ....

I created a sandbox demo here

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