简体   繁体   English

检查日期是否存在于对象数组中

[英]Check if Date exist in array of Object

I have an array of date i want to sort it and get only the recent Date 我有一个日期数组,我想对其进行排序并仅获取最近的日期

[
  "Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
  "Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
  "Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
  "Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
  "Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
  "Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
  "Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
  "Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
  "Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
  "Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"
]

shouldDisplayDate(date: Date) {
  datesFiltered = [];
  const array = this.users.map(a => a.date)
  for (const date of array) {
    if (!this.datesFiltered.find(d => new Date(d).setHours(0, 0, 0) ===
        new Date(dateString).setHours(0, 0, 0))) {
      this.datesFiltered.push(new Date(dateString).toString())
    }
  }
}

Result : 结果:

[
  Mon Jul 16 2018 15:32:50 GMT+0200 (Central European Summer Time),
  Fri Jul 13 2018 09:33:46 GMT+0200 (Central European Summer Time), 
  Thu Jul 12 2018 13:41:59 GMT+0200 (Central European Summer Time) 
]

So I want to check if I enter Mon Jul 16 2018 11:40:28 GMT+0200 (CEST) is in the Array of Object or not? 所以我想检查我是否输入Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)是否在对象数组中?

To see if two dates are equal using === you'll have to use .getTime() on them first (see this answer for more info ) 要使用===查看两个日期是否相等,您必须首先对它们使用.getTime()有关更多信息,请参见此答案

An example of doing a simple date sort and finding if a date exists in your array are below. 下面是执行简单的日期排序并查找数组中是否存在日期的示例。

To check if the date exists in the array I first convert a string date (the one we're searching for existence) to a Date obj. 为了检查日期是否存在于数组中,我首先将字符串日期(我们正在寻找存在的日期 )转换为Date obj。 Then I loop through the array of existing dates and convert them one at a time and use Date.getTime() on each date to see if they are equal, if so, the function will return true. 然后,我遍历现有日期数组,一次将其转换一次,然后在每个日期上使用Date.getTime()来查看它们是否相等,如果相等,则该函数将返回true。

 const dates = [ "Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)", "Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)", "Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)", "Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)", "Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)", "Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)", "Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)", "Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)", "Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)", "Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)" ]; // simple date sort let sortedDates = dates.sort(function(a, b) { return new Date(b) - new Date(a); }); console.log(`sorted Dates array is: ${JSON.stringify(sortedDates, null, 2)}`); console.log(`date exists in array? ${isDateInArray("Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)")}`); function isDateInArray(dateString) { let dateExists = false; let date = new Date(dateString); dates.forEach(function(arrayDateString) { let arrayDate = new Date(arrayDateString); if(date.getTime() === arrayDate.getTime()){ dateExists = true; } }); return dateExists; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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