简体   繁体   English

如何在 Javascript 中从数组中获取连续/顺序日期?

[英]How to Get Consecutive / Sequential Dates from Array in Javascript?

What I want?我想要的是?

Form groups of dates that are consecutive.形成连续的日期组。

What consecutive means?连续是什么意思?

15-05-2020, 16-05-2020, 17-05-2020... 15-05-2020, 16-05-2020, 17-05-2020...

How we group them?我们如何分组?

[
  [4114696980000,4114783380000,4114869780000],
  [4120485780000,4120572180000,4120658580000,4120744980000]
]

So in each array we have groups of dates that are consecutive.因此,在每个数组中,我们都有一组连续的日期。

What dates are not consecutive?哪些日期不连续?

26-05-2020 and 28-05-2020 because after day 26, the next one is 27-05-2020, so we would obtain two arrays, one for 26-05-2020 and other for 28-05-2020. 26-05-202028-05-2020因为在第 26 天之后,下一个是 27-05-2020,所以我们将获得两个数组,一个是 26-05-2020,另一个是 28-05-2020。

Do I have a function to do that?我有这样做的功能吗? Yes.是的。

Is that function working?那个功能好用吗? No.不。

Why?为什么? All my days are consecutive, but it gives me two arrays (groups) and I cant get why It does that.我所有的日子都是连续的,但它给了我两个数组(组),我不明白为什么会这样。

CODE:代码:

 var arr = [4128175380000,4128261780000,4128348180000, 4128434580000,4128520980000,4128607380000, 4128697380000,4128783780000,4128870180000], i = 0, result = arr.reduce(function(stack, b) { var cur = stack[i], a = cur ? cur[cur.length-1] : 0; if (b - a > 86400000) { i++; } if (!stack[i]) stack[i] = []; stack[i].push(b); return stack; }, []); console.log(result);

The result should be just one array with all dates because they are consecutive.结果应该只是一个包含所有日期的数组,因为它们是连续的。 . .

Lets clarify consecutive days with this snippet converting the dates to readable form:让我们使用此代码段将日期转换为可读形式来澄清连续天数:

 var arr = [4128175380000,4128261780000,4128348180000, 4128434580000,4128520980000,4128607380000, 4128697380000,4128783780000,4128870180000]; function getStringDatesFormat(arr) { for (var i=0; i < arr.length; i++) { console.log(new Date(arr[i]).toDateString()); } } getStringDatesFormat(arr);

As you can see in the results, all the days are consecutive, there is no day left between so why it breaks them into two arrays the above function code?在结果中可以看到,所有的日子都是连续的,中间没有一天,那么为什么上面的函数代码将它们分成两个数组?

Edit : Output from the above编辑:上面的输出

Mon Oct 25 2100 20:23:00 GMT+0100 (British Summer Time)
Tue Oct 26 2100 20:23:00 GMT+0100 (British Summer Time)
Wed Oct 27 2100 20:23:00 GMT+0100 (British Summer Time)
Thu Oct 28 2100 20:23:00 GMT+0100 (British Summer Time)
Fri Oct 29 2100 20:23:00 GMT+0100 (British Summer Time)
Sat Oct 30 2100 20:23:00 GMT+0100 (British Summer Time)
Sun Oct 31 2100 20:23:00 GMT+0000 (Greenwich Mean Time)
Mon Nov 01 2100 20:23:00 GMT+0000 (Greenwich Mean Time)
Tue Nov 02 2100 20:23:00 GMT+0000 (Greenwich Mean Time)

Given your definition of consecutive and the dates you've provided, it is expected that your function is creating two child arrays.鉴于您对连续的定义和您提供的日期,预计您的函数将创建两个子数组。

4128697380000 - 4128607380000 = 90000000

Since this is greater than 86400000, these days aren't consecutive (according to your condition).由于这大于 86400000,这些天不是连续的(根据您的情况)。

If you convert them to dates, they are indeed consecutive;如果将它们转换为日期,它们确实是连续的; but as points in time they're separated by more than 86400000 milliseconds.但作为时间点,它们相隔超过 86400000 毫秒。

To get the results you expect, you need to convert them to dates first and check if they are consecutive.要获得您期望的结果,您需要先将它们转换为日期并检查它们是否连续。 For that you can use a library like date-fns or momentjs .为此,您可以使用诸如date- fnsmomentjs 之类的库。

For example, with date-fns you could check for consecutive dates like this:例如,使用date-fns您可以检查连续日期,如下所示:

if(isAfter(b, a)) {
    i++;
}

Assuming you've already mapped the values to dates using fromUnixTime假设您已经使用fromUnixTime将值映射到日期

Your question is basically, how does one group numbers from a list by whether they are consecutive to one another?您的问题基本上是,一组如何从列表中通过它们是否彼此连续来编号?

It's ambiguous exactly what you mean by consecutive;你所说的连续到底是什么意思是模棱两可的; assuming you mean consecutive on the number line as opposed to in the array, this will work.假设您的意思是在数轴上连续而不是在数组中,这将起作用。 Note there is nothing special about dates.请注意,日期没有什么特别之处。 It will work for all numbers.它适用于所有数字。

 const numbers = [123, 124, 0, 125, 1, 2, 4, 6, 2, 9, 10, 11, 14]; const groups = numbers.reduce((groups, number, i) => { const group = groups.find((group) => group.find((n) => Math.abs(n - number) === 1)); group ? group.push(number) : groups.push([number]); return (i === numbers.length - 1) ? groups .map((g) => g.sort((a, b) => a - b)) .sort(([a], [b]) => a - b) : groups; }, []); console.log(groups);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM