Javascript Data Type How to - Convert single dimensional array into multi dimensional array








Question

We would like to know how to convert single dimensional array into multi dimensional array.

Answer


<!DOCTYPE html>
<html>
<body>
  <script type='text/javascript'>
var ids = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32];
var array_of_sets = [];
while( ids.length ) {<!--  w  w  w .  j ava  2 s. c  o m-->
    array_of_sets.push( ids.splice(0,10) );
}

console.log( array_of_sets );

document.write(array_of_sets.join('<br><br>'));

</script>
</body>
</html>

The code above is rendered as follows: