Sorting Nested Array Recently on SO there was this question

i am trying to get sequence index of each array of array like I have
an element of the array and that 5 arrays have multiple elements and I
want to get the first index of each array then the second and so.
below is my array data const arrData = [[1,2,4,6],[3,8,7],[12,13],[],[9,10]];
and my expected outout [1,3,12,9,2,8,13,10,4,7,6]

This is very basic but I found it interesting and tried to solve it.
I solved it like this, I thought it is easy to understand and to read but in reasons that I go in every nested array through all elements maybe it is not the best solution.
For example if I have one array with length 100 and all the other ones are just have length 1.
What do you think about my code?


const arr = [
  [1, 2, 4, 6],
  [3, 8, 7],
  [12, 13],
  [],
  [9, 10]
];

let biggestLength = Number.MIN_VALUE;
for (let i = 0; i &lt; arr.length; i++) {
  if (arr[i].length &gt; biggestLength) {
    biggestLength = arr[i].length;
  }
}

let result = [];
for (let l = 0; l &lt; biggestLength; l++) {
  for (let k = 0; k &lt; arr.length; k++) {
    if (arr[k][l] !== undefined) {
      result.push(arr[k][l])
    }
  }
}


console.log(result)
Overall your code is readable and it seems reasonably clear what it does. As you said, the code is somewhat inefficient if one of the arrays is much larger than the rest, but for your example (and honestly, even if you had a thousand items in one array), the time cost would probably be negligible, unless the number of arrays in arr is very very large.
Number.MIN_VALUE
Number.MIN_VALUE is not what you think it is. It represents the smallest positive number that can be represented, not the most negative number. As a result, if you use let arr = []; you end up with a biggestLength that is slightly more than 0 and run your second loop... only to figure out that you do not have items. Or arrays, This in itself does not break anything, but you could just initialise biggestLength to 0 and things would work out fine.
Optimising
As I said earlier, the amount of time it takes to do this operation is negligible, even if you have 100 items in the first array. With 11 items over 4 arrays (16 operations) it takes me 0.010ms, and with 107 items over 4 arrays (400 operations) it takes me 0.025ms.


const arr = [
  [1, 2, 4, 6],
  [3, 8, 7],
  [12, 13],
  [],
  [9, 10]
];

function getUnfoldedArray(arr) {
  let biggestLength = Number.MIN_VALUE;
  for (let i = 0; i &lt; arr.length; i++) {
    if (arr[i].length &gt; biggestLength) {
      biggestLength = arr[i].length;
    }
  }

  let result = [];
  for (let l = 0; l &lt; biggestLength; l++) {
    for (let k = 0; k &lt; arr.length; k++) {
      if (arr[k][l] !== undefined) {
        result.push(arr[k][l])
      }
    }
  }
  
  return result;
}

console.time('First run is always slower');
getUnfoldedArray(arr);
console.timeEnd('First run is always slower');

console.time('time1');
const result = getUnfoldedArray(arr);
console.timeEnd('time1'); // 0.010ms

arr[0] = [];
for (let i = 0; i &lt; 100; i++) {
  arr[0].push(i);
}

console.time('time2');
const result2 = getUnfoldedArray(arr);
console.timeEnd('time2'); // 0.025ms

console.log(result);
console.log(result2);



If you don't mind destroying the array you are doing stuff on, you can potentially use Array.shift(). You could also splice out any empty arrays then, but keep in mind that loops will mess up when you do that. Overall your code would probably get more complicated for not a lot of gain.