如何在 NodeJS 中将数组分成相等的部分

How to divide an array into equal parts in NodeJS

我是 nodejs 的新手,我需要划分一个包含 x 轴上的日期及其在 y 轴上的点的数组,并尝试使用数组来绘制图形来存储 x 和 y 轴的数据来做到这一点我正在这样做:

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

它将存储从开始日期到结束日期的所有日期,现在我需要将它分成几周,所以我找到周数:

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

现在要在哪个日期有一个点所以我这样做:

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 ++;
          }
        }     
    }
}

现在这将给我如下输出:

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

现在我需要将这个数组划分为 arr 到周和 我试过了

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

但它在 temparray 中存储为:

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

我需要如下临时数组:

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

使用fillmap的解决方案:

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

您可以调整它以使用日期

正如@Alberto Trindade Tavares 的回答,我只是通过 :

/* 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);
  });
}

只需一行简单的代码,您就可以获得比上面提供的更快up to 3 times的解决方案:

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}