1

I m Trying to remove the duplicate date from an array of Date like

let dates = ["date1","date2","date1","date3"];

I convert dates into Set but it's doesn't remove duplicate, but when I try it with other datatypes instead of Date in work, Where is the problem?

let uniq = dates => [...new Set(dates)];

manoj
  • 5,235
  • 7
  • 24
  • 45
  • Your `date1` and the others are simple dates like `2018-06-07 11:42` ? – Suren Srapyan Jun 07 '18 at 07:42
  • 1
    If the dates are Objects, then they'll never `===` each other if they were created separately. If you want to deduplicate like that, you could convert the dates to timestamps first – CertainPerformance Jun 07 '18 at 07:43
  • https://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other – baao Jun 07 '18 at 07:45
  • @SurenSrapyan not date object contain date with 0 hour, minute, sec and milisecond – manoj Jun 07 '18 at 08:12

1 Answers1

3

Because your dates are objects they will we compared via references. Two objects can't be equal though they have all equal properties.

const a = { name: 'Test' } and const b = { name = 'Test' } have identical values but their references (address in the memory) are not equal. So why Set does not work in your case.

You can work with their string representations. Strings are going to be compared via their value. const a = 'Test' and const b = 'Test' are identical in this case. Map them using toString function and then insert them into the Set. Same dates will have the same string representations and so they will not be unique.

const dates = [
    new Date(), // Will be the same as the below 3 dates at runtime
    new Date(), 
    new Date(), 
    new Date(), 
    new Date(2015, 1, 1)
];

const uniqueDates = [...new Set(dates.map(date => date.toString()))];

console.log(uniqueDates);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • "The Set object lets you store unique values of any type, whether primitive values or object references." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set – Sid Jun 07 '18 at 07:47
  • 2
    With objects it will compare references – Suren Srapyan Jun 07 '18 at 07:47
  • Cleverness is all well and good, but likely the OP has no idea why the above does what it does, nor why the Dates are now Strings. – RobG Jun 07 '18 at 07:48
  • @RobG Adding some explanation – Suren Srapyan Jun 07 '18 at 07:48
  • @SurenSrapyan i just posted the reference for the OP :) – Sid Jun 07 '18 at 07:48
  • @Sid Ok. I think you write it to me ) – Suren Srapyan Jun 07 '18 at 07:52
  • @SurenSrapyan it returns date like Tue Jun 05 2018 05:45:00 GMT+0545 (+0545) I want date object within array, but it helps me for finding solution I m working in that – manoj Jun 07 '18 at 08:14
  • @SurenSrapyanya exactly i do the same and i m testing it now – manoj Jun 07 '18 at 08:17
  • 2
    Rather than parsing strings, consider using time values to avoid all parsing: `[...new Set(dates.map(date => +date))].map(tv => new Date(tv))`. Also worth noting that the resulting array will be new Dates, not references to the original Dates (which may be an issue). – RobG Jun 07 '18 at 09:17