匹配相似的(MOMMJS)在两个阵列(MOMMJ)的日期之间的日期为一个新数组

发布于 2025-02-07 11:35:29 字数 1648 浏览 3 评论 0原文

简而言之:我正在寻找firstDatelist中的所有日期,这些日期在seconddatelist中的匹配日期在某个阈值之内(可以在几秒钟到分钟之间有所不同)(<

我正在努力弄清楚一些基本的东西 两个日期列表(IM使用MOMMJS)。 (这是用于在HighCharts图上显示特定点)。

我必须浏览第一个日期中的每个日期,并找到第二个日期列表的匹配日期。问题是,我可以获得 相似 的日期,因此它们可以放弃一点(以前只需要获得精确(ISSAME)的日期,但是现在无法使用,因为一些日期停止了几秒钟到几分钟)。

我正在检查每个之间的差异,但是我不完全理解 获得我需要的日期。

目前,它返回到“二人组”中的第一个日期(命名令人困惑,对不起)

任何想法只能在两个列表中获得匹配(最接近每个)的日期?

我适合在两个阵列中使用foreach吗?我不完全知道如何仅将 类似的 追溯到新数组中。

我认为我应该在第一个数组中过滤,以便我返回与我想要的新日期相匹配的新数组?我不太确定了..

const closestDatePoints: GraphPoint[] = [];
let closestPointDifference: number | null = null;

firstDateList?.forEach((firstDate, index) => {
  const formattedFirstDate = moment(firstDate[0]); // this just gets the date from this firstDate object

  secondDateList?.forEach((secondDate, index) => {
    // const isSame = date.isSame(formattedFirstDate);

    const differenceInMinutes = Math.abs(
      moment(secondDate)?.diff(formattedFirstDate, 'minutes')
    );

    if (
      closestPointDifference === null ||
      closestPointDifference > differenceInMinutes
    ) {
      closestPointDifference = differenceInMinutes;

      // I realize that this is pushing dates that I also do not 
      // want - its pushing all dates until the first, firstDate, and 
      // stopping once it hits that first firstDate. Don't know how 
      // to make it return ***only*** the dates I need.
      closestDatePoints.push(firstDate);
    }


  });
});

In Short: I'm looking for all the dates in firstDateList that have a matching date in secondDateList within some threshold (which can differ from seconds to minutes) (Thanks for @gloo's comment).

I'm struggling to figure out what seems like some basic stuff with
two lists of dates (im using momentjs). (This is for displaying specific points on a highcharts graph).

I have to go through each of the first dates, and find matching dates with the second list of dates. The issue is, I can get dates that are similar to each other, so they can be off by a bit (previously just needed to get exact (isSame) dates, but that wont work now as some dates are off by a few seconds to minutes).

I'm checking the diff between each, but I don't fully understand to only get the dates that I need.

Right now its returning every point up to the first date in the "secondDateList" (the naming is confusing, sorry)

any ideas as to get only the matching (closest to each) dates within both lists?

Am I right for using forEach for both arrays? I dont fully know how to only push the similar dates into a new array.

I think I should filter in the first array so I return the new array of dates that match what I want? I'm not too sure anymore..

const closestDatePoints: GraphPoint[] = [];
let closestPointDifference: number | null = null;

firstDateList?.forEach((firstDate, index) => {
  const formattedFirstDate = moment(firstDate[0]); // this just gets the date from this firstDate object

  secondDateList?.forEach((secondDate, index) => {
    // const isSame = date.isSame(formattedFirstDate);

    const differenceInMinutes = Math.abs(
      moment(secondDate)?.diff(formattedFirstDate, 'minutes')
    );

    if (
      closestPointDifference === null ||
      closestPointDifference > differenceInMinutes
    ) {
      closestPointDifference = differenceInMinutes;

      // I realize that this is pushing dates that I also do not 
      // want - its pushing all dates until the first, firstDate, and 
      // stopping once it hits that first firstDate. Don't know how 
      // to make it return ***only*** the dates I need.
      closestDatePoints.push(firstDate);
    }


  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2025-02-14 11:35:29

这个问题就像找到两个数组的交点一样,只是值可以在阈值之内而不是严格相等。这是一种简短而简单的幼稚方法:

  const closestDates = firstDateList.filter((date1) =>
    secondDateList.some((date2) => Math.abs(date1.diff(date2)) < threshold)
  );

我无法在实际日期进行测试,这远非最快的方法,所以我不确定它是否适用于您的情况,而是对您的情况阵列这很可读。

如果您需要更快的性能,则需要首先确保对两个数组进行排序以防止一些冗余比较

This problem is like finding the intersection of two arrays, except the values can be within a threshold instead of being strictly equal. Here is a naive approach that is pretty short and simple:

  const closestDates = firstDateList.filter((date1) =>
    secondDateList.some((date2) => Math.abs(date1.diff(date2)) < threshold)
  );

I have not been able to test this on real dates yet and this is far from the fastest approach, so I'm not sure if it will work for your case, but for small arrays this is pretty readable.

If you need faster performance you would need to first ensure both arrays are sorted to prevent some redundant comparisons

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文