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

128
Visualizações
How to dynamicaly regroup each element of multiple arrays with same length

I would like to dynamicaly regroup each element with same index of multiple (no precise number) arrays with the same length in a array.

Example :

var arrayOfarray =[
['a','b','c','d','e','f'],
['h','i','j','k','l','m'],
] 

/* 
expectedResult = [['a','h'],['b','i'],['c','j'],['d','k'],['e','l'],['f','m']]
*/

Thank you

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

var a = [1, 2, 3]
var b = ['a', 'b', 'c']

var c = a.map(function(e, i) {
  return [e, b[i]];
});

console.log(c)
about 4 years ago · Santiago Gelvez Relatório

0

var result = [];
for(var i = 0; i < arrayOfarray.length; i++){
    for(var j = 0; j < arrayOfarray[i].length; j++){
        result.push([arrayOfarray[i][j], arrayOfarray[(i+1)%2][j]]);
    }
} 
about 4 years ago · Santiago Gelvez Relatório

0

it's just 2 nested for loops ... nothing fancy

the thing we have to realize is, that the length of the array we want to return is the same as the length of the nested arrays of the array we pass to the function.

and each nested array in the array we want to return has the same length as the main array we pass to the function.

so ists basically like

passedArray.length === returnedArray[i].length //true
passedArray[i].length === returnedArray.length //true

function x(arr) {
  const retVal = [];
  for (let i = 0, subArr; i < arr[0].length; i++) {
    subArr = []
    for (let j = 0; j < arr.length; j++) {
      subArr.push(arr[j][i])
    }
    retVal.push(subArr)
  }
  return retVal
}

const arrayOfarray = [['a', 'b', 'c', 'd', 'e', 'f'], ['h', 'i', 'j', 'k', 'l', 'm']];

console.log(x(arrayOfarray));

about 4 years ago · Santiago Gelvez 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