const years = [2016, 2017, 2017, 2016, 2019, 2018, 2019];
// ES5
const distinct = (value, index, self) => {
return self.indexOf(value) === index;
};
var distinctYears = years.filter(distinct); //return [2016, 2017, 2019, 2018]
distinctYears
// ES6
var distinctYears = [...new Set(years)];
distinctYears
// It will work on primitive values only, need some more customized methods if an array of dates
// The following code will not work as Set compares objects by reference
const dates = [
new Date(2018, 8, 1),
new Date(2018, 9, 1),
new Date(2018, 8, 1),
new Date(2018, 9, 1)
];
[...new Set(dates)]