Lor*_*ini 6 arrays reduce node.js
我看到很多帖子/问题都解决了这个问题,所以我认为这并不是微不足道的。我是一个初学者,正在寻找更优雅的解决方案。
我需要将这种包含1分钟详细数据的数组减少为5分钟数据。只需计算 5 分钟内连续值的总和,然后重新创建一个较短的数组。那么时间戳“created_at”应该是 5 分钟时间段结束的时间点。
let array = [
{ steps: 40, created_at: '2022-09-03T11:36:00.000Z' },
{ steps: 13, created_at: '2022-09-03T11:37:00.000Z' },
{ steps: 40, created_at: '2022-09-03T11:38:00.000Z' },
{ steps: 40, created_at: '2022-09-03T11:39:00.000Z' },
{ steps: 34, created_at: '2022-09-03T11:40:00.000Z' },
{ steps: 86, created_at: '2022-09-03T11:41:00.000Z' },
{ steps: 23, created_at: '2022-09-03T11:42:00.000Z' },
{ steps: 78, created_at: '2022-09-03T11:43:00.000Z' },
{ steps: 67, created_at: '2022-09-03T11:44:00.000Z' },
{ steps: 80, created_at: '2022-09-03T11:45:00.000Z' },
{ steps: 34, created_at: '2022-09-03T11:46:00.000Z' },
{ steps: 64, created_at: '2022-09-03T11:47:00.000Z' },
{ steps: 32, created_at: '2022-09-03T11:48:00.000Z' },
{ steps: 78, created_at: '2022-09-03T11:49:00.000Z' },
{ steps: 45, created_at: '2022-09-03T11:50:00.000Z' }
]
Run Code Online (Sandbox Code Playgroud)
我认为我的解决方案太复杂了:
const moment = require(`moment`);
const newArray = array.map(
(item)=> {
const timestamp = moment(item.created_at).valueOf();
const timeStampgroup = Math.ceil((timestamp)/300000);
return {...item, timeStampgroup: timeStampgroup}
}
);
//console.log(newArray);
const reducedArray = Array.from(newArray.reduce(
(m, {timeStampgroup: timeStampgroup, steps}) => m.set(timeStampgroup, (m.get(timeStampgroup) || 0) + steps), new Map
), ([timeStampgroup, steps]) => ({timeStampgroup, steps}));
//console.log(reducedArray);
const result = reducedArray.map(entry => ({steps : entry.steps, created_at : moment(entry.timeStampgroup*300000).toISOString()}));
console.log(result);
[
{ steps: 167, created_at: '2022-09-03T11:40:00.000Z' },
{ steps: 334, created_at: '2022-09-03T11:45:00.000Z' },
{ steps: 253, created_at: '2022-09-03T11:50:00.000Z' }
]
Run Code Online (Sandbox Code Playgroud)
有没有人看到一种不太复杂的方法来实现相同的结果,也许一次性?
多谢 !
洛伦佐
像这样?
const moment = require(`moment`)
require('moment-round')
const array = [
{ steps: 40, created_at: '2022-09-03T11:36:00.000Z' },
{ steps: 13, created_at: '2022-09-03T11:37:00.000Z' },
{ steps: 40, created_at: '2022-09-03T11:38:00.000Z' },
{ steps: 40, created_at: '2022-09-03T11:39:00.000Z' },
{ steps: 34, created_at: '2022-09-03T11:40:00.000Z' },
{ steps: 86, created_at: '2022-09-03T11:41:00.000Z' },
{ steps: 23, created_at: '2022-09-03T11:42:00.000Z' },
{ steps: 78, created_at: '2022-09-03T11:43:00.000Z' },
{ steps: 67, created_at: '2022-09-03T11:44:00.000Z' },
{ steps: 80, created_at: '2022-09-03T11:45:00.000Z' },
{ steps: 34, created_at: '2022-09-03T11:46:00.000Z' },
{ steps: 64, created_at: '2022-09-03T11:47:00.000Z' },
{ steps: 32, created_at: '2022-09-03T11:48:00.000Z' },
{ steps: 78, created_at: '2022-09-03T11:49:00.000Z' },
{ steps: 45, created_at: '2022-09-03T11:50:00.000Z' },
]
const result = Array.from(
array.reduce((m, item) => {
const key = moment(item.created_at).ceil(5, 'minutes').toISOString()
return m.set(key, (m.get(key) || 0) + item.steps)
}, new Map()),
([created_at, steps]) => ({ created_at, steps })
)
console.log(result)
Run Code Online (Sandbox Code Playgroud)