JavaScript How to divide an array into equal parts in NodeJS divide array into equal parts,divide array into equal parts

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 :

while(startDate <= endDate)
{
  arr.push({x:startDate.toISOString().slice(0,10),y: 0});
  startDate.setDate(startDate.getDate() + 1); 
} 

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 :

var   Weeks = Math.round((endDate - startDate) / (7 * 24 * 60 * 60 * 1000));

now to get on which date there is a point so i do :

for (var i = doc.length - 1; i >= 0; i--) {
   for (var j = arr.length - 1; j >= 0; j--) {
        if (arr[j].x == doc[i].deal_end_date) {
            arr[j].y ++;
          }
        }     
    }
}

now this will give me an output as below :

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 } ]

now i need to divide this array i.e arr into weeks and i tried

var i,j,temparray,chunk = Weeks;
for (i=0,j=arr.length; i<j; i+=chunk) {
    temparray = arr.slice(i,i+chunk);
}

but it stores in temparray as :

temparray: [ { x: '2017-07-31', y: 0 } ]

and i need my temparray as below :

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 }]
Answer:1

A solution using fill and map:

function splitArray(array, chunkSize) {
  return Array(Math.ceil(array.length/chunkSize)).fill().map(function(_,i) {
    return array.slice(i * chunkSize, i * chunkSize + chunkSize);
  });
}

var results = splitArray([1,2,3,4,5,6,7,8], 3);
console.log(results);
Answer:2

With one simple line of code, you may get the solution that may work up to 3 times faster than provided above:

const src = [...'abcdefghijklmnop'];

const chunkArr = (arr, size) => arr.reduceRight((res,_,__,self) => [...res, self.splice(0, size)],[]);

console.log(chunkArr(src, 3));
.as-console-wrapper {max-height: 100% !important; top 0}
Answer:3

I'm working on a network visualization using Three.js and am having trouble determining why my ray casting implementation is not identifying the correct points (Full example and source). More ...

I'm working on a network visualization using Three.js and am having trouble determining why my ray casting implementation is not identifying the correct points (Full example and source). More ...

Sounds easy probably, but not for a beginner programmer :) I have a simple 3 field form with a submit button and a clear button. This is for a homework assignment, and we have been tasked to get the ...

Sounds easy probably, but not for a beginner programmer :) I have a simple 3 field form with a submit button and a clear button. This is for a homework assignment, and we have been tasked to get the ...

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

  1. show read more in jquery
  2. show read more in php
  3. show read more in wordpress
  4. today show read more today
  5. show hide read more javascript
  6. wordpress show read more link
  7. how to show read more in html
  8. javascript hide/show read more for long text
  9. how to show read more link in wordpress post
  10. how to show read more in textview in android
  11. read more show less
  12. read more show hide text
  13. read more show less jquery
  14. read more show less javascript
  15. read more show hide jquery
  16. read more show less css
  17. read more show less bootstrap
  18. read more show hide div
  19. wp-show-posts-read-more
  20. read more. show less. reply