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.