How to divide an array into equal parts in NodeJS

2019-07-24 04:30发布

问题:

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

回答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);

You can adapt it to use dates



回答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}



回答3:

As answered by @Alberto Trindade Tavares i just do it by :

/* arr is my original array */
var ressu = splitArray(arr, 7);

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