Home > Back-end >  Nodejs: how to make intersection of two arrays of dates
Nodejs: how to make intersection of two arrays of dates

Time:01-10

How to do an intersection of matrices correctly. I'm working in nodejs and I'm trying to compare two arrays to get the same elements i.e. an "array intersection"

This is a pretty common question, actually, and even though I've searched and applied the resolutions you mention, I still don't understand why I can't. I have two arrays of dates, and the dates I'm handling are in the same format, so I'm ruling out the possibility that it won't work for that reason.

I used the most common solution, which was to use the filter() method to filter the array and validate via the include() method if array B includes any of the elements of array A.

Then try the same thing but with the some() method to check the array until it returns a true value.

In my case, what I am doing is through a range of dates, I generate a new array with the dates from the start to the end of the range.

The following code returns an array in "someDates" which is the result of trying to intercept the "currentWeek" array with the "holidaysDates" array

But I don't understand why it doesn't work?

function getArrayOfRangeDate(startDate, endDate) {

    let dates: any[] = [];
    let currentDate = startDate;

    while (currentDate <= endDate) {
        dates.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate()   1);
    }

    return dates;
}

            users.map(user => {

                let user_id = user.id;
                let username = user.username;
                let modality = user.modality.name;

                isHoliday.forEach((schedule: AttendanceSchedule) => {

                    let startDate1 = new Date(datestart);
                    let endDate1 = new Date(dateend);
                    let startDate2 = new Date(schedule.date_start);
                    let endDate2 = new Date(schedule.date_end);

                    const currentWeek: any[] = getArrayOfRangeDate(startDate1, endDate1);
                    const holidaysDates: any[] = getArrayOfRangeDate(startDate2, endDate2);

                    const results = currentWeek.filter(o1 => holidaysDates.some(o2 => o1 === o2));
                    // console.log('sameDates', results);

                    newListOfSchedule.push({
                        user_id: user_id,
                        username: username,
                        modality: modality,
                        currentWeek: currentWeek,
                        holidaysDates: holidaysDates,
                        sameDates: results
                    });

                });

DEMO simple

const arrayA = [
"2022-12-19T00:00:00.000Z",
"2022-12-20T00:00:00.000Z",
"2022-12-21T00:00:00.000Z",
"2022-12-22T00:00:00.000Z",
"2022-12-23T00:00:00.000Z",
"2022-12-24T00:00:00.000Z",
"2022-12-25T00:00:00.000Z"
]

const arrayB = [
"2022-12-19T00:00:00.000Z",
"2022-12-20T00:00:00.000Z"
]

const results = arrayA.filter(o1 => arrayB.some(o2 => o1 === o2));

console.log('results', results)

In conclusion, what I want to do is go through array A and compare it to array B to return the elements that are the same in both arrays.

I leave you some links where they explain the interception of arrangements that I find interesting to share

CodePudding user response:

You're comparing two Date objects. In your example snippet, you're comparing two strings. When comparing objects with ===, you're actually comparing by reference. In other words, two objects are the same, only if they are the same reference:

const a = new Date();
const b = new Date();

a === b // false, different object references
a === a // true, exact same reference

So when comparing dates, you need to convert them to a primitive (like a string or number):

const results = currentWeek.filter(o1 => holidaysDates.some(o2 => o1.getTime() === o2.getTime()));

I've used getTime as an example which returns a number representing the date as the number of milliseconds since the epoch.

  • Related