Home > Net >  Matching similar (momentJs) dates between two arrays of (momentJs) dates into a new array
Matching similar (momentJs) dates between two arrays of (momentJs) dates into a new array

Time:06-15

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);
    }


  });
});

CodePudding user response:

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

  • Related