How do I hold arrays in a collection in javascript?

How do I hold arrays in a collection in javascript ?

I am trying to do this. Not sure about syntax.

Is it possible ?

var bigarray= [ ];
bigarray[0]={array1,array2,array3} // how do I add these ?
bigarray[1]={array4,array5,array6}
bigarray[2]={array7,array8,array9}

Hi there winzip,

does this help…

<script>
 (function() {
    'use strict';
   var array1 = [ 1, 2, 3 ], array2 = [ 4, 5, 6 ], array3 = [ 7, 8, 9 ],
       array4 = [ 10, 11, 12 ], array5 = [ 13, 14, 15 ], array6 = [ 16, 17, 18 ],
       array7 = [ 18, 20, 21 ], array8 = [ 22, 23, 24 ], array9 = [ 25, 26, 27],
       bigarray = [ [ array1, array2, array3 ], [ array4, array5, array6 ], [array7, array8, array9 ] ];

       console.log( bigarray[ 1 ][ 1 ] );
  }());
</script>

coothead

1 Like

Is the below correct syntax ? I’m stuck here. I’ll add array to bigarray dynamically.

bigarray[ i ].push(someArray)

Hi there winzip

try it like this…

<script>
 (function() {
    'use strict';

   var array1 = [ 1, 2, 3 ], array2 = [ 4, 5, 6 ], array3 = [ 7, 8, 9 ],
       array4 = [ 10, 11, 12 ], array5 = [ 13, 14, 15 ], array6 = [ 16, 17, 18 ],
       array7 = [ 18, 20, 21 ],array8 = [ 22, 23, 24 ], array9 = [ 25, 26, 27],

       someArray = [ 'this is some array!' ],

       bigarray = [ [ array1, array2, array3 ], [ array4, array5, array6 ], [array7, array8, array9 ] ];

       bigarray.push( someArray );

       console.log( bigarray[ 1 ][ 1 ] );

       console.log( bigarray[ bigarray.length - 1 ] );
  }());
</script>

…but bear in mind that javascript is not my forte
and there may be more modern solutions. :winky:

coothead

Slight difference:
bigarray.push pushes a new element in the first dimension.
bigarray[i].push pushes a new element in the second dimension of the ith vector of the first dimension.
bigarray[i][j].push pushes a new element in the third dimension of the jth vector of the second dimenion in the ith vector of the first dimension.
etc etc etc.
dimensions can go as deep as your memory limit allows.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.