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

135
Vistas
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 Respuestas
Responde la pregunta

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 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