I am trying to create an array with objects holding day and time. I am looping over a source where there may be duplicates so I want to check each time that I don't already have the current day and time stored.
However, I keep ending up with duplicates. So I figure the array.indexOf maybe doesn't work with objects?
movies.forEach(function(movie){
if (days.indexOf(movie.day) !== -1) { //if the movie's day exists in our array of available days...
possibleMovies.push(movie);
//create a day/time object for this movie
dt = {day: movie.day, time: movie.time};
//unless we already have this day and time stored away, do so
if (possibleTimes.indexOf(dt) === -1) {
possibleTimes.push(dt);
}
}
});
What possibleTimes holds after the loop is done:
[ { day: '01', time: '18:00' },
{ day: '01', time: '16:00' },
{ day: '01', time: '18:00' },
{ day: '01', time: '16:00' } ]
I would expect line three and four not to be there...
---------- UPDATE ----------
I changed
dt = {day: movie.day, time: movie.time};
into this
dt = JSON.stringify({day: movie.day, time: movie.time});
and it works as expected. just need to JSON.parse once I retrieve the data.