im试图缩小nodejs中的数据以获取图表,但不知道哪种方法是最好的。我想创建一个获取原始数据并将函数的总值缩小X天的函数,例如:
数据:
{ time: '2018-10-29', total: 8 }
{ time: '2018-10-30', total: 9 }
{ time: '2018-10-31', total: 10 }
{ time: '2018-11-1', total: 4 }
{ time: '2018-11-2', total: 21 }
{ time: '2018-11-3', total: 21 }
{ time: '2018-11-4', total: 4 }
{ time: '2018-11-5', total: 7 }
{ time: '2018-11-6', total: 14 }
{ time: '2018-11-7', total: 11 }
{ time: '2018-11-8', total: 15 }
{ time: '2018-11-9', total: 14 }
{ time: '2018-11-10', total: 23 }
{ time: '2018-11-11', total: 13 }
{ time: '2018-11-12', total: 6 }
{ time: '2018-11-13', total: 14 }
{ time: '2018-11-14', total: 17 }
{ time: '2018-11-15', total: 16 }
{ time: '2018-11-16', total: 16 }
{ time: '2018-11-17', total: 21 }
{ time: '2018-11-18', total: 6 }
{ time: '2018-11-19', total: 12 }
{ time: '2018-11-20', total: 11 }
{ time: '2018-11-21', total: 15 }
{ time: '2018-11-22', total: 15 }
{ time: '2018-11-23', total: 14 }
{ time: '2018-11-24', total: 25 }
{ time: '2018-11-25', total: 1 }
如果我想将此数据缩小到10天,它将是28/10 = Math.round(2.8),因此每3个都将合并并返回。但问题是我如何将其编码。
感谢您的任何建议
答案 0 :(得分:0)
您可以尝试这样的事情:
const getDataInBucket = (bucketIndex, data) => data.filter((obj, i) => i >= bucketSize*bucketIndex && i<bucketSize*(bucketIndex+1));
然后您可以这样称呼它:
getDataInBucket(0, data); // first 10 elements
getDataInBucket(1, data); // second 10 elements
getDataInBucket(2, data); // rest 8 elements