Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!