Partitioning and array in JavaScript - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Partitioning and array in JavaScript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from w w  w  . java2s  . c o  m
Array.prototype.partition = function(length) {
  var result = [];
  for(var i = 0; i < this.length; i++) {
    if(i % length === 0) 
         result.push([]);
    result[result.length - 1].push(this[i]);
  }
  return result;
};
console.log(JSON.stringify([1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,9].partition(3)));
    });

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

Related Tutorials