Kibou
Kibou

Reputation: 63

How to loop OVER several arrays

I have n number of arrays. Same amount of rows in each of them: Array0, Array1, ..., Arrayn.

Now I want to loop through all of them and ultimately join them. Transpose each Array to a column of the new created ResultArray

for (var ID = 0; ID < n+1; ID++) {
    for (var i = 0; i < ("Array" + ID).length; i++) {
      ResultArray[i][ID] = ("Array" + ID)[i]
        }
  }

Here is an example of what I want:

Array0 = [Apple, Banana, Chicken]
Array1 = [5, 7, 8]
Array2 = [High, Low, Low]
ResultArray = [[Apple, 5, High], [Banana, 7, Low], [Chicken, 8, Low]]

Upvotes: 1

Views: 287

Answers (3)

Ori Drori
Ori Drori

Reputation: 193358

Use Array.reduce() to find the longest array. Use Array.map() to iterate the longest array, and get the row index. Then use a second Array.map() to get the row items from the arrays.

const zip = (...arrs) => arrs
  .reduce((r, c) => c.length > r.length ? c : r, []) // get the longest array
  .map((_, i) => arrs.map(a => a[i])) // map the longest array to get the current row number, then create the row

const arr1 = ["Apple", "Banana", "Chicken"], arr2 = [5, 7, 8], arr3 = ["High", "Low", "Low"]

const result = zip(arr1, arr2, arr3)

console.log(result)

Upvotes: 0

Akrion
Akrion

Reputation: 18525

You can also solve this in a more concise way with Array.reduce:

const arr1 = ["Apple", "Banana", "Chicken"], arr2 = [5, 7, 8], arr3 = ["High", "Low", "Low"],
      data = [arr1, arr2, arr3]

let result = data.reduce((acc, c) => 
 (c.forEach((x,k) => acc[k].push(x)), acc), Array.from({length: data.length}, () => []))

console.log(result)

Or in a more readable form:

const arr1 = ["Apple", "Banana", "Chicken"], arr2 = [5, 7, 8], arr3 = ["High", "Low", "Low"],
      data = [arr1, arr2, arr3]

let result = data.reduce((acc, c) => {
  c.forEach((x,i) => acc[i].push(x))
  return acc
}, Array.from({length: data.length}, () => []))

console.log(result)

The idea is to merge those arrays and then do Array.reduce where the accumulator is already in the form of [[],[],[]] so we just populate each of the child arrays.

Upvotes: 0

Tanaike
Tanaike

Reputation: 201778

  • You have the following arrays.

        Array0 = ["Apple", "Banana", "Chicken"];
        Array1 = [5, 7, 8];
        Array2 = ["High", "Low", "Low"];
    
  • You want to convert above arrays to the following array.

        [["Apple",5,"High"],["Banana",7,"Low"],["Chicken",8,"Low"]]
    
  • From your sample array, the array length of each array is the same.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modified script 1:

var Array0 = ["Apple", "Banana", "Chicken"];
var Array1 = [5, 7, 8];
var Array2 = ["High", "Low", "Low"];

var arrays = [Array0, Array1, Array2]; // Please prepare this.

var res = [];
for (var i = 0; i < arrays[0].length; i++) {
  var temp = [];
  for (var j = 0; j < arrays.length; j++) {
    var ar = arrays[j];
    temp.push(ar[i]);
  }
  res.push(temp);
}
Logger.log(res)

Modified script 2:

var Array0 = ["Apple", "Banana", "Chicken"];
var Array1 = [5, 7, 8];
var Array2 = ["High", "Low", "Low"];

var numberOfArrays = 3; // Please prepare this. In your case, it's 3.

var res = [];
for (var i = 0; i < Array0.length; i++) {
  var temp = [];
  for (var j = 0; j < numberOfArrays; j++) {
    var ar = eval("Array" + j);
    temp.push(ar[i]);
  }
  res.push(temp);
}
Logger.log(res)

Note:

  • Please select one of above scripts for your situation.

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 2

Related Questions