0

I have an array that i have made have two of the same value, i have made a function to check the array for duplicates but for some reason it never goes into the if statement. ( if(array[i] == array[j]) ).

when i is 0 and j is 1 they should be matching.

let sessions = [
    {"title": "Lecture", "sessionTime": new Date(2021, 8, 28, 9), "staff": "example"},
    {"title": "fake", "sessionTime": new Date(2021, 8, 28, 9), "staff": "example"},
    {"title": "Lab 1", "sessionTime": new Date(2021, 8, 28, 14), "staff": "example"},
    {"title": "Lab 2", "sessionTime": new Date(2021, 9, 1, 11), "staff": "example"}
];

errorCheck1 = function(sessions) {
    for (let i = 0; i < sessions.length; i++) {
        for (let j = 0; j < sessions.length; j++) {
            if (i != j) {
                if (sessions[i].sessionTime == sessions[j].sessionTime) {
                    $("#errors").append("<li>" + sessions[i].title + " clashes with " + sessions[j].title +"</li>");
                }
            }
        }
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Josh Bent
  • 23
  • 1
  • 4
  • You're comparing two objects (Date objects in this case), [which will never match](https://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other), try converting to a comparable value first. `sessions[i].sessionTime.getTime() == sessions[j].sessionTime.getTime()` – pilchard Oct 29 '21 at 23:28
  • 1
    @pilchard 100% right, thanks! massive overlook from me. – Josh Bent Oct 29 '21 at 23:38

0 Answers0