Created
December 24, 2020 19:31
-
-
Save DoctorDerek/a32c460f1e9869e41ae55141da22a92d to your computer and use it in GitHub Desktop.
How to Find Unique Dates in an Array in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const date1 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| console.log(date1) | |
| // Output: Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time) | |
| // The date2 object will have a different object reference. | |
| const date2 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| // The date3 variable will have the same object reference as date1. | |
| const date3 = date1 | |
| // It's possible to add duplicate object references to an array: | |
| const dateArray = [date1, date2, date3, date2] | |
| const uniqueDates = Array.from(new Set(dateArray)) | |
| // Using Array.from() is the same as [...(new Set(dateArray)] | |
| // | |
| // We're left with the unique object references for the Date objects: | |
| console.log(uniqueDates) | |
| // Output: [Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time), Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time)] | |
| // | |
| // The uniqueDates array contains the date1 and date2 object references: | |
| console.log(`date1: ${uniqueDates[0] === date1}`) // date1: true | |
| console.log(`date2: ${uniqueDates[1] === date2}`) // date2: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const date4 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| console.log(date4) | |
| // Output: Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time) | |
| // The date5 object will have a different object reference | |
| const date5 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| // This array has 2 different object references. | |
| const dateArray2 = [date4, date5] | |
| // Use JSON.stringify() and JSON.parse() in a .map() call with new Date(): | |
| const uniqueDates2 = Array.from( | |
| new Set(dateArray2.map((dateObject) => JSON.stringify(dateObject))) | |
| ).map((dateString) => new Date(JSON.parse(dateString))) | |
| // Using Array.from() is the same as [...(new Set(dateArray)] | |
| // Just one Date remains, because they had the same timestamp. | |
| console.log(uniqueDates2) | |
| // Output: [Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time)] | |
| // The uniqueDates array contains a new Date, neither date4 nor date5. | |
| console.log(`date4: ${uniqueDates2[0] === date4}`) // date4: true | |
| console.log(`date5: ${uniqueDates2[0] === date5}`) // date5: false | |
| // The new date has the same timestamp in milliseconds as date4 or date5. | |
| console.log(`${uniqueDates2[0].valueOf() === date4.valueOf()}`) // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const date6 = new Date("3005-01-01 00:01") // Happy New Year! 🎉🎈🎊🥳 | |
| console.log(date6) | |
| // Output: Date Tue Jan 01 3005 00:01:00 GMT-0500 (Eastern Standard Time) | |
| // Now the date7 object is one minute ahead of the date6 object. | |
| const date7 = new Date("3005-01-01 00:02") // Happy New Year! 🎉🎈🎊🥳 | |
| console.log(date7) | |
| // Output: Date Tue Jan 01 3005 00:02:00 GMT-0500 (Eastern Standard Time) | |
| const dateArray3 = [date6, date7] | |
| // Calling .toDateString() on each Date object compares the calendar date. | |
| const uniqueDates3 = Array.from( | |
| new Set(dateArray3.map((dateObject) => dateObject.toDateString())) | |
| ).map((dateString) => new Date(dateString)) | |
| // Using Array.from() is the same as [...(new Set(dateArray)] | |
| // Just one Date remains, because they had the same human-readable date. | |
| console.log(uniqueDates3) | |
| // Output: [Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time)] | |
| // Note that the time was lost for the new Date, so we're back to midnight. | |
| console.log(`${uniqueDates3[0].valueOf() === date6.valueOf()}`) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment