简体   繁体   中英

Filter array of dates (allow only one date entry per day)

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?

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);

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)

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)"]

Using the first chars something like this should work:

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM