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

195
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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