如何在给定的开始日期和结束日期之间的数组中添加丢失的数据条目 [英] How to add missing data entries in an array between a given start date and end date
问题描述
我有一个名为plot_data
的数组.因为我有两个对象属性,日期x
和值y
.这些日期实际上是过去的日期.在该数组中,多个地方的某些地方缺少一些日期.因此,无论错过什么日期,我都希望将其添加到数组中.
I have an array named plot_data
. In that I have two object properties date x
and value y
. The dates are actually past dates. In that array a few dates are missing somewhere at multiple places. So, whatever dates are missed I want to add them in the array.
plot_data数组就是这样
plot_data array is like this
0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Tue Apr 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
3: {x: Wed Apr 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
4: {x: Sat Apr 14 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
5: {x: Sun Apr 15 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
我想要这样的数组输出.
I want the output of the array like this.
0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Sun Apr 08 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
3: {x: Mon Apr 09 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
4: {x: Tue Jun 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
5: {x: Wed Jun 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
6: {x: Thu Jun 12 2018 10:00:00 GMT-0500 (Central Daylight Time), y: null}
7: {x: Fri Jun 13 2018 11:00:00 GMT-0500 (Central Daylight Time), y: null}
8: {x: Sat Jun 14 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
9: {x: Sun Jun 15 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}
我不知道如何执行此操作,因为我们必须考虑一个月中的约会次数.请帮忙.
I am not getting idea how to do this, since we have to consider number of dates in a month. Please help.
推荐答案
您可以通过添加24小时来获取下一个日期,那么您就不必关心该月中的日期了.
You can get the next date by just adding the 24 hours then you will not have to care about the date in the month.
我已经删除了时区,您可以根据需要进行设置.
I have removed the timezone, you can set the same as per your requirement.
let arr = [{x: 'Fri Apr 06 2018 22:30:00', y: 93.9},
{x: 'Sat Apr 07 2018 09:00:00', y: 69.3},
{x: 'Tue Apr 10 2018 10:00:00', y: 71.2},
{x: 'Wed Apr 11 2018 11:00:00', y: 67.2},
{x: 'Sat Apr 14 2018 10:00:00', y: 71.2},
{x: 'Sun Apr 15 2018 11:00:00', y: 67.2}]
let i=0;
while(i < arr.length-1) {
let nextday = new Date(new Date(arr[i].x).getTime() + 24 * 60 * 60 * 1000);
if(nextday.getDay() != (new Date(arr[i+1].x)).getDay()) {
arr.splice(i+1, 0, {x: nextday.toString(), y: null});
};
i++
}
console.log(arr)
这篇关于如何在给定的开始日期和结束日期之间的数组中添加丢失的数据条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!