假設我有一個物件陣列,每個物件代表某個時間和各自的價格值:
const prices = [
{
price_time: '2021-10-13T16:30:00 00:00',
price: 3.5,
},
{
price_time: '2021-10-13T18:15:00 00:00"',
price: 5,
},
{
price_time: '2021-10-13T19:15:00 00:00"',
price: 6.5,
},
];
現在假設我需要以 15 分鐘的時間間隔輸出一個所有價格串列,每個小時存在于陣列price_time的第一個和最后一個元素的值之間prices。
定義的結果間隔串列16:30,18:15然后19:15時間將是:
[
{ price_time: '2021-10-13T16:30:00.000Z', price: 3.5 }, // first elem in the prices array
{ price_time: '2021-10-13T16:45:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:00:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:15:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:30:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:45:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T18:00:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T18:15:00.000Z', price: 5 }, // second elem in the prices array
{ price_time: '2021-10-13T18:30:00.000Z', price: 5 },
{ price_time: '2021-10-13T18:45:00.000Z', price: 5 },
{ price_time: '2021-10-13T19:00:00.000Z', price: 5 },
{ price_time: '2021-10-13T19:15:00.000Z', price: 6.5 } // last elem in the prices array
]
實作這一目標的最佳方法是什么?我一直在努力得到它通過遍歷陣列值,讓每兩個元素之間的小時,然后填充在做00:00,00:15,00:30和00:45間隔:
const _ = require('date-fns');
const prices = [
{ price_time: '2021-10-13T16:30:00 00:00', price: 3.5 },
{ price_time: '2021-10-13T18:15:00 00:00',price: 5 },
{ price_time: '2021-10-13T19:15:00 00:00', price: 6.5 },
];
function addMissingIntervals() {
const allIntervals = [];
prices.forEach((item, index) => {
if (index !== prices.length - 1) { // if this isn't the last loop item
const start = _.parseISO(item.price_time); // the datetime of the current loop item
const end = _.parseISO(prices[index 1].price_time); // the datetime of the next loop item
const allHours = _.eachHourOfInterval({ start: start, end: end }); // all the hours between these two
allHours.map((hour) => {
allIntervals.push({ price_time: hour, price: item.price }) // insert the start of the hour
allIntervals.push(
...[15, 30, 45].map((t) => { // for each hour, add 15m, 30m & 45m
return { price_time: _.addMinutes(hour, t), price: item.price };
}),
);
});
}
});
// Adding the last item from `prices` as well
allIntervals.push(prices[prices.length - 1]);
return allIntervals;
}
console.log(addMissingIntervals());
但是,后者的結果包括所有小時的所有時間間隔,包括開始/結束日期之前或之后的時間間隔:
[
{ price_time: '2021-10-13T16:00:00.000Z', price: 3.5 }, // should not be included
{ price_time: '2021-10-13T16:15:00.000Z', price: 3.5 }, // should not be included
{ price_time: '2021-10-13T16:30:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T16:45:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:00:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:15:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:30:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T17:45:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T18:00:00.000Z', price: 3.5 },
{ price_time: '2021-10-13T18:15:00.000Z', price: 3.5 }, // should not be included
{ price_time: '2021-10-13T18:30:00.000Z', price: 3.5 }, // should not be included
{ price_time: '2021-10-13T18:45:00.000Z', price: 3.5 }, // should not be included
{ price_time: '2021-10-13T18:00:00.000Z', price: 5 }, // should not be included
{ price_time: '2021-10-13T18:15:00.000Z', price: 5 },
{ price_time: '2021-10-13T18:30:00.000Z', price: 5 },
{ price_time: '2021-10-13T18:45:00.000Z', price: 5 },
{ price_time: '2021-10-13T19:00:00.000Z', price: 5 },
{ price_time: '2021-10-13T19:15:00.000Z', price: 5 }, // should not be included
{ price_time: '2021-10-13T19:30:00.000Z', price: 5 }, // should not be included
{ price_time: '2021-10-13T19:45:00.000Z', price: 5 }, // should not be included
{ price_time: '2021-10-13T19:15:00.000Z', price: 6.5 }
]
uj5u.com熱心網友回復:
我設法通過修改這個答案來解決這個問題(它使用 moment.js 而不是 date-fns)。
本質上,我在每兩個日期之間運行一個回圈,將每個回圈專案時間四舍五入到最接近的 15 分鐘,并為該間隔分配正確的價格。在回圈終止后,原始價格陣列中的最后一項被忽略并添加到結果陣列中:
const _ = require('date-fns');
const prices = [
{ price_time: '2021-10-13T16:30:00 00:00', price: 3.5 },
{ price_time: '2021-10-13T18:15:00 00:00', price: 5 },
{ price_time: '2021-10-13T19:15:00 00:00', price: 6.5 },
];
function intervalsGroup(startDate, endDate, price) {
// Round to the nearest 15
let current = _.setMinutes(startDate, Math.ceil(_.getMinutes(startDate) / 15) * 15);
let group = [];
while (endDate > current) {
group.push({ price_time: current, price: price })
current = _.addMinutes(current, 15);
}
return group;
}
function addMissingIntervals() {
const allIntervals = [];
prices.forEach((item, index) => {
if (index !== prices.length - 1) { // if this isn't the last loop item
const start = _.parseISO(item.price_time); // the datetime of the current loop item
const end = _.parseISO(prices[index 1].price_time); // the datetime of the next loop item
allIntervals.push(...intervalsGroup(start, end, item.price)); // generate the 15m intervals between these two days
}
});
allIntervals.push(prices[prices.length - 1]);
return allIntervals;
}
console.log(addMissingIntervals());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/356606.html
