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

188
Visualizações
Add one more level of nested arrays in array of arrays with initial data JavaScript

I have the following array of arrays:

    [
      [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],
      [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],
      [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]
    ]

I have to split the second item of every nested array and create two new arrays with them and the first item of a nested array like this:

    [
       [
          [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1' ],
          [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '2' ]
       ],
       [
          [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3' ],
          [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '4' ]
       ],
       [
          [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5' ],
          [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '6' ]
       ]
    ]

I need this kind of nested array because I use it for a bulk insert in nodeJS.

My current method that I am using to create this array looks like this:

    console.log(teams.map(team => {
            let initialArray = []
            let firstPlayer = [uuidv4(), team[0], team[1].split(' & ')[0]]
            let secondPlayer = [uuidv4(), team[0], team[1].split(' & ')[1]] 
    
            data = (initialArray.concat(firstPlayer)).concat(secondPlayer)
    
            return data
        }))

And the response is like that:

    [
      [
        'da4d27fb-06e3-4075-aaba-be799d793bbd',
        '3b5ec76c-a501-4edf-b97c-2526d6d3e147',
        '1',
        'df869f69-76fb-41bc-af59-f86c1c623cfb',
        '3b5ec76c-a501-4edf-b97c-2526d6d3e147',
        '2'
      ],
      [
        'fb7c5620-6f20-4586-a82a-30c0ea39022e',
        '1f7a07f4-45aa-4a35-ad42-d3f187ee804a',
        '3',
        '1d30d9d7-3d63-4309-a7d3-5bc6abe17500',
        '1f7a07f4-45aa-4a35-ad42-d3f187ee804a',
        '4'
      ],
      [
        '433159a1-ee21-4f91-ae6e-69fb359b747b',
        'c8a4eead-79ee-4d4b-a16d-68723798d338',
        '5',
        '8321a976-9bae-408d-810b-f5508012d58e',
        'c8a4eead-79ee-4d4b-a16d-68723798d338',
        '6'
      ]
    ]

I have also tried a method using reduce found in this stackoverflow question: Return multiple arrays using "map" function

If somebody knows a method for this, or you need further explanations, let me know.

Thank you!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try this approach

const data = [[ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],[ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],[ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]];

const result = data.map(([guid, teams]) => {
    const [team1, team2] = teams.split(' & ');
    return [[guid, team1], [guid, team2]];
});

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Same result but with oneliner

const data = [[ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],[ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],[ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]];

const result = data.map(([guid, teams]) => teams.split(' & ').map((team) => [guid, team]));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Juan Pablo Isaza Relatório

0

This would also work.

Using Array.prototype.reduce()

const input = [
  [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],
  [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],
  [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]
]

const output = input.reduce((prev, [uuid, combinedValue]) => {
  const [first,second] = combinedValue.split(" & ");
  prev.push([[uuid, first], [uuid, second]]);
  return prev;
},[])

console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

You Just need 3 lines of Code

const source = [
  ["3b5ec76c-a501-4edf-b97c-2526d6d3e147", "1 & 2"],
  ["1f7a07f4-45aa-4a35-ad42-d3f187ee804a", "3 & 4"],
  ["c8a4eead-79ee-4d4b-a16d-68723798d338", "5 & 6"],
];

const destination = source.map(team => {
    const options = team[1].split('&'); 
    newSubDestinationArray = options.map(item => ["your uuid id", team[0], item]); 
    return newSubDestinationArray;
})


console.log(destination)
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