Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a Map by key

I have a map:

for (let jobInArray of jobs) {

  // lets me get rid of time, outputs to YYYY-MM-dd
  let deliveryDateString: string = new Date(jobInArray.DeliveryDate).toLocaleDateString("en-us");

  if (dateJobsMap.has(deliveryDateString)) {
    let jobsForDate: IDeliveryJob[] = dateJobsMap.get(deliveryDateString);
    jobsForDate.push(jobInArray);
  }
  else {
    dateJobsMap.set(deliveryDateString, [jobInArray]);
  }
}

Gives me:

dateJobsSortedMap:  Map(12) {"8/23/2018" => Array(14), "8/22/2018" => Array(19),...

I want to remove any entry that is older then the current date.

I tried:

let foobar = [...dateJobsMap.keys()].filter((item: string) => {
  console.log("item: ", item);

  let temp = new Date(item);

  return temp >= new Date();
});

But that returns empty. It should return today's items (8/23/2018).

Any suggestions?

like image 243
Holden1515 Avatar asked Apr 23 '26 16:04

Holden1515


1 Answers

The problem is that when you create a date from the m/dd/yyyy template, its hours, minutes and seconds will always be 00:00:00, but when you create a date with an empty input parameter - there will always be a current hours, minutes and seconds. So you can do this:

let foobar = [...dateJobsMap.keys()].filter((item: string) => {
  console.log("item: ", item);

  let temp = new Date(item + " 23:59:59");

  return temp >= new Date();
});
like image 188
stdob-- Avatar answered Apr 26 '26 06:04

stdob--



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!