Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!