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

134
Views
Splitting an array with N arrays of a value pair, into set N sets

Any help or suggestion on how to achieve please?

I have a group of 8 teams

    teamArray  =['Team_1', 'Team_2', 'Team_3', 'Team_4', 'Team_5', 'Team_6', 'Team_7', 'Team_8']

and use the following function to pair each team which give 28 pairs

    let pairs = teamArray.flatMap(
        (v, i) => teamArray.slice(i+1).map( w => [v,w] )
    );

Now I want to take the 28 pairs and split into 7 arrays of with 4 pairs, while each array cannot have an pair with one value more than once?

an example of the desired output:

result  = [
[['Team_1', 'Team_2'], ['Team_3', 'Team_4'], ['Team_5', 'Team_6'], ['Team_7', 'Team_8']],
[['Team_1', 'Team_3'], ['Team_2', 'Team_4'], ['Team_5', 'Team_7'], ['Team_6', 'Team_8']],
...]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Seems like a task for the reduce method of an array:

result = pairs.reduce((accumulator, currentPair) => {
    let match = accumulator.find(array => !array.some(pair => currentPair.some(ci => pair.some(i => ci === i))));
    if (match !== undefined) {
        match.push(currentPair);
        return accumulator;
    }
    else {
        accumulator.push([currentPair]);
        return accumulator;
    }
}, []);
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!