Jordan Ramstad Jordan Ramstad - 1 month ago 7
Javascript Question

Unique Array for dates Javascript

I see this question asked quite often for regular javascript arrays, however none of the answers seems to work if its an array of dates.

I can likely figure this out through trial an error but I do see some benefit to others if I ask.

Basically if you have a javascript array of dates that might have duplicates and need to filter into an array with no duplicates what is the best way to go about that?

I have tried the ES6 solution of

Array.from(new Set(arr))
but it just returns the same array.

Also I tried

Array.prototype.unique = function() {
var a = [];
for (var i=0, l=this.length; i<l; i++)
if (a.indexOf(this[i]) === -1)
a.push(this[i]);
return a;
}


both came from Unique values in an array

However neither worked, looks like
indexOf
does not work on date objects.

Here is how my array is generated atm

//this is an array generated from ajax data,
//its a year over year comparison with a separate year,
//so to create a reliable date objects I force it to use the same year.
data.map(d => {
dp = new Date(Date.parse(d.date + '-' + d.year));
dp.setFullYear(2000);
return dp;
})


It is about 100 or so different days, but it always ends up with about 350 index's.

Answer

If you compare two dates via ===, you compare the references of the two date objects. Two objects that represent the same date still are different objects.

Instead, compare the timestamps from Date.prototype.getTime():

function isDateInArray(needle, haystack) {
  for (var i = 0; i < haystack.length; i++) {
    if (needle.getTime() === haystack[i].getTime()) {
      return true;
    }
  }
  return false;
}

var dates = [
  new Date('October 1, 2016 12:00:00 GMT+0000'),
  new Date('October 2, 2016 12:00:00 GMT+0000'),
  new Date('October 3, 2016 12:00:00 GMT+0000'),
  new Date('October 2, 2016 12:00:00 GMT+0000')
];

var uniqueDates = [];
for (var i = 0; i < dates.length; i++) {
  if (!isDateInArray(dates[i], uniqueDates)) {
    uniqueDates.push(dates[i]);
  }
}

console.log(uniqueDates);

Optimization and error handling is up to you.

Comments