Tyding up Nested Arrays in JavaScript

hi there,

i have this array filled with arrays, it looks like so :

[ [ { intervals: 159, remMinute: 5 },   //[0][0]
    { intervals: 33, remMinute: 7 },        //[0][1]
    { intervals: 54, remMinute: 27 },     //[0][2]
    { intervals: 100, remMinute: 20 },   //[0][3]
    { intervals: 61, remMinute: 14 },      //[0][4]
    { intervals: 65, remMinute: 41 } ],   //[0][5]
  [ { intervals: 83, remMinute: 13 },     //[1][0]
    { intervals: 82, remMinute: 9 },          //[1][1]
    { intervals: 115, remMinute: 23 },     //[1][2]
    { intervals: 90, remMinute: 27 } ],     //[1][3]
  [ { intervals: 86, remMinute: 15 },     //[2][0]
    { intervals: 58, remMinute: 34 },     //[2][1]
    { intervals: 90, remMinute: 5 },         //[2][2]
    { intervals: 60, remMinute: 23 },        //[2][3]
    { intervals: 106, remMinute: 15 },     //[2][4]
    { intervals: 34, remMinute: 5 },            //[2][5]
    { intervals: 74, remMinute: 37 } ] ]     //[2][6]

i would like to have, for each index of the secondary arrays, a fresh new array :

newArray ={array[0][0], array array[1][0],array[2][0]…}; an array with all thirst index
newArray1 = {array[0][1], array array[1][1],array[2][1]…}; an array with all second index
newArray2 ={array[0][2], array array[1][2],array[2][2]…}; an array with all third index

i can’t figure how to loop in the loop, it must be very easy but i ’ m obviously missing something .
i did succeed in building a function returning the length of the bigger secondary array (betting it will be usefull) :

 function taille(array)
    {
      var a=0;
    for (a = 0 ; a< array.length ; a++)
    {
      var z = 0;
      if (array[a].length>z)
      {
        z = array[a].length; 
      }
     
    }
 return (z); 
    }   

sorry for the kind of question…
by advance thank you!

in facrt i m missing how to programaticly create variables with unic names ,
i manage to do that witch is not elegant for sure :

   for ( d=0 ; d<toutesNuits.length ; d++)
    {
        cycle1.push( toutesNuits[d][0]);
        cycle2 .push( toutesNuits[d][1]);
        cycle3 .push( toutesNuits[d][2]);
        cycle4 .push( toutesNuits[d][3]);
        cycle5 .push( toutesNuits[d][4]);
        cycle6 .push( toutesNuits[d][5]);
        cycle7.push( toutesNuits[d][6]); 
        cycle8 .push( toutesNuits[d][7]);
        cycle9 .push( toutesNuits[d][8]);
        cycle10 .push( toutesNuits[d][9]);
     //  cycle10 .push( toutesNuits[d][10]);
       //bigger--;
    }

if you want ten different variables you need to create ten different variables

but if you want you could have an object or an array with inside the arrays you want, and then you can avoid repeating the same line ten times

let orderedArray = [];
for (let i = 0; i < arr.length; i++) {
   for (let j = 0; j < arr[i].length; j++) {
      if (!orderedArray[i]) {orderedArray[j] = []}
      orderedArray[j].push(arr[i][j])
   }
}
let obj = {};
for (let i = 0; i < arr.length; i++) {
   for (let j = 0; j < arr[i].length; j++) {
      if (!obj['cycle' + (j+1)]) {obj['cycle' + (j+1)] = []}
      obj['cycle'+(j+1)].push(arr[i][j])
   }
}

or something like this, I have not tested it, but in principles it should work, and it can be tidied up a bit

1 Like