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

132
Visualizações
Grab first element from three arrays and put them inside its own array, then do the same for the second element and so forth in javascript

I have three arrays

arr1 = ["a1", "a2", "a3"]

arr2 = ["b1", "b2", "b3"]

arr3 = ["c1", "c2", "c3"]

and I want three new arrays to look like so

arr4 = ["a1", "b1", "c1"]

arr5 = ["a2", "b2", "c2"]

arr6 = ["a3", "b3", "c3"]

How can I accomplish getting these new arrays in JavaScript?

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

0

You Can use two loop to do that but you will add all arrays in one array to do the loop and we extract new arrays in another array

Here is your code

let arr1 = ["a1", "a2", "a3"],
arr2 = ["b1", "b2", "b3"],
arr3 = ["c1", "c2", "c3"];

We add all arrays in one variable

let allArr = [arr1, arr2, arr3];

We make a array to give all new arrays in

let resultArr = [];

We make the first loop to loop in each array

for (let i = 0; i < allArr.length; i++) {

We make a array called res to make the new arrays

let res = [];

We make another loop on allArr Variable but for change we use a foreach loop

  allArr.forEach((arr) => {
    res.push(arr[i]);
  });

Then We add res after we make it to the resultArr Vairable

resultArr.push(res);

And We close out loop

}

Here is you code

let arr1 = ["a1", "a2", "a3"],
  arr2 = ["b1", "b2", "b3"],
  arr3 = ["c1", "c2", "c3"];

let allArr = [arr1, arr2, arr3];
let resultArr = [];
for (let i = 0; i < allArr.length; i++) {
  let res = [];
  allArr.forEach((arr) => {
    res.push(arr[i]);
  });
  resultArr.push(res);
}

I add this loop to log the array in console only. To see that working live

resultArr.forEach((arr) => {
  console.log(arr);
});

let arr1 = ["a1", "a2", "a3"],
  arr2 = ["b1", "b2", "b3"],
  arr3 = ["c1", "c2", "c3"];

let allArr = [arr1, arr2, arr3];
let resultArr = [];
for (let i = 0; i < allArr.length; i++) {
  let res = [];
  allArr.forEach((arr) => {
    res.push(arr[i]);
  });
  resultArr.push(res);
}
resultArr.forEach((arr) => {
  console.log(arr);
});

I hope to be good in my explain :)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's one way - put the source arrays in one variable, use the length of the shortest source array as the iterator length and create the new variables on the fly.

let arr1 = ["a1", "a2", "a3"]
let arr2 = ["b1", "b2", "b3"]
let arr3 = ["c1", "c2", "c3"]

// just in case they're not all the same length, we'll use the shortest one as the iterator length

let ct = 0,
  st = 4,
  arrs = [arr1, arr2, arr3],
  len = Math.min(arr1.length, arr2.length, arr3.length);
while (ct < len) {
  this[`arr${st+ct}`] = arrs.map(a => a[ct]);
  ct++;
}

console.log(arr5)

about 4 years ago · Juan Pablo Isaza Relatório

0

let mixedArr = [arr1, arr2, arr3];
let res = [];
for (i=0;i<mixedArr.length;i++) {
    res.push(mixedArr.map(arr => arr[i]))
}
console.log(res)

And then you can just grab what you need from the finished array

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