2

I am having a hard time figuring this out :(

I have an array of dates:

let myDatesArray = [
"Sun Oct 01 2017 05:30:00 GMT+0530 (IST)",
"Mon Oct 02 2017 05:30:00 GMT+0530 (IST)",
"Mon Oct 02 2017 06:30:00 GMT+0530 (IST)",
"Tue Oct 03 2017 05:30:00 GMT+0530 (IST)",
"Wed Oct 04 2017 05:30:00 GMT+0530 (IST)",
"Thu Oct 05 2017 05:30:00 GMT+0530 (IST)",
"Fri Oct 06 2017 05:30:00 GMT+0530 (IST)",
"Sat Oct 07 2017 05:30:00 GMT+0530 (IST)"]

However, I am only allowed to have one date entry per day! The array can have TWO dates for Monday, October 2nd but I would have to discard one (hence one per day). I tried using myDatesArray.filter(el => ????) but can't figure it out. Any ideas on how I could achieve this?

Walter Monecke
  • 2,386
  • 1
  • 19
  • 52

5 Answers5

5

You could take a Set with a substring of the date strings.

let array = ["Sun Oct 01 2017 05:30:00 GMT+0530 (IST)", "Mon Oct 02 2017 05:30:00 GMT+0530 (IST)", "Mon Oct 02 2017 06:30:00 GMT+0530 (IST)", "Tue Oct 03 2017 05:30:00 GMT+0530 (IST)", "Wed Oct 04 2017 05:30:00 GMT+0530 (IST)", "Thu Oct 05 2017 05:30:00 GMT+0530 (IST)", "Fri Oct 06 2017 05:30:00 GMT+0530 (IST)", "Sat Oct 07 2017 05:30:00 GMT+0530 (IST)"],
    seen = new Set,
    result = array.filter(s => !seen.has(s.slice(0, 15)) && seen.add(s.slice(0, 15)));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can just compare the first 15 characters of each timestamp. The following keeps the last instance of duplicate days:

let dates = [
"Sun Oct 01 2017 05:30:00 GMT+0530 (IST)",
"Mon Oct 02 2017 05:30:00 GMT+0530 (IST)",
"Mon Oct 02 2017 06:30:00 GMT+0530 (IST)",
"Tue Oct 03 2017 05:30:00 GMT+0530 (IST)",
"Wed Oct 04 2017 05:30:00 GMT+0530 (IST)",
"Wed Oct 04 2017 06:30:00 GMT+0530 (IST)",
"Wed Oct 04 2017 07:30:00 GMT+0530 (IST)",
"Thu Oct 05 2017 05:30:00 GMT+0530 (IST)",
"Fri Oct 06 2017 05:30:00 GMT+0530 (IST)",
"Sat Oct 07 2017 05:30:00 GMT+0530 (IST)"]

let unique = dates.filter((d, i, arr) => 
  d.substr(0,15) != (arr[i+1]  || '').substr(0,15)
); 

console.log(unique)
RobG
  • 142,382
  • 31
  • 172
  • 209
1
const singleDate = myDatesArray.reduce((acc,str) => {
  acc[new Date(str).setHours(0,0,0,0)] = str;
  return acc;
},{});

const filteredDates = Object.values(singleDate);

// ["Sun Oct 01 2017 05:30:00 GMT+0530 (IST)", "Mon Oct 02 2017 06:30:00 GMT+0530 (IST)", "Tue Oct 03 2017 05:30:00 GMT+0530 (IST)", "Wed Oct 04 2017 05:30:00 GMT+0530 (IST)", "Thu Oct 05 2017 05:30:00 GMT+0530 (IST)", "Fri Oct 06 2017 05:30:00 GMT+0530 (IST)", "Sat Oct 07 2017 05:30:00 GMT+0530 (IST)"]
Ashish Kumar
  • 212
  • 1
  • 7
  • This won't work across months, *getDate* returns just the day in the month so will treat 1 Jan and 1 Feb as duplicates. :-( – RobG Nov 19 '19 at 12:20
  • @RobG agreed. we can always add an extra check for the month as well. – Ashish Kumar Nov 19 '19 at 12:24
  • 1
    You could make the key `new Date(str).setHours(0,0,0,0)` but the usual caveat for [using the built–in parser](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) applies. Your method also assumes the host is set to the same timezone as the timestamps (IST). :-) – RobG Nov 19 '19 at 12:27
  • @RobG i guess my updated solution will fix the issue. – Ashish Kumar Nov 19 '19 at 12:29
  • It won't fix the timezone issue. – RobG Nov 19 '19 at 12:29
0

Using the first chars something like this should work:

let dateMap = {};
myDatesArray.forEach(d => dateMap[d.substr(0,15)] = d);
myDatesArray = Object.values(dateMap);
0

Use Set to level to trim array from duplicate values

// array of dates
    let myDatesArray = [
      "Sun Oct 01 2017 05:30:00 GMT+0530 (IST)",
      "Mon Oct 02 2017 05:30:00 GMT+0530 (IST)",
      "Mon Oct 02 2017 06:30:00 GMT+0530 (IST)",
      "Tue Oct 03 2017 05:30:00 GMT+0530 (IST)",
      "Wed Oct 04 2017 05:30:00 GMT+0530 (IST)",
      "Thu Oct 05 2017 05:30:00 GMT+0530 (IST)",
      "Fri Oct 06 2017 05:30:00 GMT+0530 (IST)",
      "Sat Oct 07 2017 05:30:00 GMT+0530 (IST)"
    ];

// make a Set from that array. Set automaticaly removes all duplicate values
const dateSet = new Set(myDatesArray);

// then make array out of Set.
let myDatesArrayLeveled = Array.from(dateSet);

// you now have array of unique date values
console.log(myDatesArrayLeveled);
JozeV
  • 616
  • 3
  • 14
  • 27
  • The OP only wants to consider the date part, not the time so one of the Mondays should be removed. Using the whole timestamp as a key doesn't do that. – RobG Nov 19 '19 at 12:31