簡體   English   中英

如何在 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 ++;
          }
        }     
    }
}

現在這將給我一個 output,如下所示:

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

我需要我的 temparray 如下:

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

您可以調整它以使用日期

只需一行簡單的代碼,您可以獲得的解決方案的運行速度可能比上面提供的速度快3倍

 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} 

正如@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);
  });
}

您也可以使用 for 循環來執行此操作。 這很容易理解。

  function splitToChunks(array, parts) {
        const result = [];
        for (let i = parts; i > 0; i--) {
            result.push(array.splice(0, Math.ceil(array.length / i)));
        }
        return result;
    }
var results = splitToChunks([1,2,3,4,5,6,7,8], 3);
console.log(results);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM