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

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

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 Denunciar

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 Denunciar

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