title: How to divide an array into equal parts in NodeJS
body: <p>I am new to nodejs and i need to divide an array which contain dates on x-axis and its points on y axis and trying to draw a graph using an array to store data for x and y axis to do that i am doing this :</p>
  
  <pre><code>while(startDate &lt;= endDate)
  {
    arr.push({x:startDate.toISOString().slice(0,10),y: 0});
    startDate.setDate(startDate.getDate() + 1); 
  } 
  </code></pre>
  
  <p>it will store all the dates from start date to ending date now i need to divide it into weeks so i am finding weeks by :</p>
  
  <pre><code>var   Weeks = Math.round((endDate - startDate) / (7 * 24 * 60 * 60 * 1000));
  </code></pre>
  
  <p>now to get on which date there is a point so i do :</p>
  
  <pre><code>for (var i = doc.length - 1; i &gt;= 0; i--) {
     for (var j = arr.length - 1; j &gt;= 0; j--) {
          if (arr[j].x == doc[i].deal_end_date) {
              arr[j].y ++;
            }
          }     
      }
  }
  </code></pre>
  
  <p>now this will give me an output as below :</p>
  
  <pre><code>startDate: 2017-07-10, endDate: 2017-07-31
  
  
  arr :
  [ { x: '2017-07-10', y: 1 },
        { x: '2017-07-11', y: 0 },
        { x: '2017-07-12', y: 0 },
        { x: '2017-07-13', y: 0 },
        { x: '2017-07-14', y: 0 },
        { x: '2017-07-15', y: 1 },
        { x: '2017-07-16', y: 0 },
        { x: '2017-07-17', y: 0 },
        { x: '2017-07-18', y: 0 },
        { x: '2017-07-19', y: 0 },
        { x: '2017-07-20', y: 0 },
        { x: '2017-07-21', y: 0 },
        { x: '2017-07-22', y: 0 },
        { x: '2017-07-23', y: 0 },
        { x: '2017-07-24', y: 0 },
        { x: '2017-07-25', y: 0 },
        { x: '2017-07-26', y: 0 },
        { x: '2017-07-27', y: 0 },
        { x: '2017-07-28', y: 0 },
        { x: '2017-07-29', y: 0 },
        { x: '2017-07-30', y: 0 },
        { x: '2017-07-31', y: 0 } ]
  </code></pre>
  
  <p>now i need to divide this array i.e arr into weeks and 
  i tried </p>
  
  <pre><code>var i,j,temparray,chunk = Weeks;
  for (i=0,j=arr.length; i&lt;j; i+=chunk) {
      temparray = arr.slice(i,i+chunk);
  }
  </code></pre>
  
  <p>but it stores in temparray as :</p>
  
  <pre><code>temparray: [ { x: '2017-07-31', y: 0 } ]
  </code></pre>
  
  <p>and i need my temparray as below :</p>
  
  <pre><code>startDate: 2017-07-01 endDate: 2017-07-28 
  Weeks: 4
  /*temparray[1] should be from arr[0] to arr[6]*/
  temparray[1] :
  [ { x: '2017-07-01', y: 0 },
    { x: '2017-07-02', y: 0 },
    { x: '2017-07-03', y: 0 },
    { x: '2017-07-04', y: 0 },
    { x: '2017-07-05', y: 1 },
    { x: '2017-07-06', y: 0 },
    { x: '2017-07-07', y: 0 }]
  /*temparray[2] should be from arr[7] to arr[13]*/
  temparray[2] :
  [ { x: '2017-07-08', y: 0 },
    { x: '2017-07-09', y: 0 },
    { x: '2017-07-10', y: 0 },
    { x: '2017-07-11', y: 0 },
    { x: '2017-07-12', y: 0 },
    { x: '2017-07-13', y: 0 },
    { x: '2017-07-14', y: 0 }]
  /*temparray[3] should be from arr[14] to arr[20]*/
  temparray[3] :
  [ { x: '2017-07-15', y: 0 },
    { x: '2017-07-16', y: 0 },
    { x: '2017-07-17', y: 0 },
    { x: '2017-07-18', y: 0 },
    { x: '2017-07-19', y: 0 },
    { x: '2017-07-20', y: 0 },
    { x: '2017-07-21', y: 0 }]
  /*temparray[4] should be from arr[21] to arr[27]*/
  temparray[4] :
  [ { x: '2017-07-22', y: 0 },
    { x: '2017-07-23', y: 0 },
    { x: '2017-07-24', y: 0 },
    { x: '2017-07-25', y: 0 },
    { x: '2017-07-26', y: 0 },
    { x: '2017-07-27', y: 0 },
    { x: '2017-07-28', y: 0 }]
  </code></pre>