【问题标题】:Get Min date and Max date from Object从对象获取最小日期和最大日期
【发布时间】:2022-10-13 17:02:44
【问题描述】:

如何从对象中找出最小和最大日期? 目前,我得到一个这样的数组:最小日期应该是“2010-02-24”,最大日期应该是“2022-10-04”。

是否有任何内置功能可以做到这一点?提前致谢。

{
       "2010":[
          {
             "id":1243,
             "eventName":"sample_01",
             "categoryType":"CUSTOM_NOTES",
             "tags":"tag19",
             "startDate":"2010-02-24",
             "endDate":"2010-02-26",
             "attachments":[
                
             ]
          }
       ],
       "2022":[
          {
             "id":1244,
             "eventName":"sample_02",
             "categoryType":"CUSTOM_NOTES",
             "tags":"tag1, tag12, tag3, tag52, tag19",
             "startDate":"2022-10-04",
             "endDate":"2022-12-12",
             "attachments":[
                
             ]
          },
          {
             "id":1245,
             "eventName":"hello_03",
             "categoryType":"CUSTOM_NOTES",
             "tags":"tag1, tag12",
             "startDate":"2022-06-01",
             "endDate":"2010-06-26",
             "attachments":[
                
             ]
          }
       ]
    }


filterEventsByDates = () => {
    const filterDateFn = (a, b) => a.startDate.localeCompare(b.startDate);
  setDateFiltersToState(filterDateFn);
}

setDateFiltersToState = (filterDateFn) => {
  this.setState(state => {
   const events = {};
   for (const [year, items] of Object.entries(state.events)) {
     events[year] = items.slice().filter(filterDateFn);
   }
  return { events };
 });
}

【问题讨论】:

  • 你试过什么?在这里分享你的代码。
  • 我尝试了使用带有 isAfter() 和 isBefore() 函数的时刻依赖的东西。但这对我不起作用。

标签: javascript reactjs react-native


【解决方案1】:

sort 将在这里完成工作,首先将所有日期打包到一个数组中:

const values = {
   "2010":[
      {
         "id":1243,
         "eventName":"sample_01",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag19",
         "startDate":"2010-02-24",
         "endDate":"2010-02-26",
         "attachments":[

         ]
      }
   ],
   "2022":[
      {
         "id":1244,
         "eventName":"sample_02",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag1, tag12, tag3, tag52, tag19",
         "startDate":"2022-10-04",
         "endDate":"2022-12-12",
         "attachments":[

         ]
      },
      {
         "id":1245,
         "eventName":"hello_03",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag1, tag12",
         "startDate":"2022-06-01",
         "endDate":"2010-06-26",
         "attachments":[

         ]
      }
   ]
};

// include startDate only
const dates = Object.values(values).flatMap(v =>
    v.map(({startDate}) => startDate)).sort();

console.log(dates[0], dates.pop());

// include startDate and endDate
const datesAny = Object.values(values).flatMap(v =>
    v.flatMap(({startDate, endDate}) => [startDate, endDate])).sort();

console.log(datesAny[0], datesAny.pop());

【讨论】:

    【解决方案2】:

    在这里您可以使用reduce 功能。 &检查日期是否更少或更多(存储在累加器中的日期)。

    const obj = {"2010":[{"id":1243,"eventName":"sample_01","categoryType":"CUSTOM_NOTES","tags":"tag19","startDate":"2010-02-24","endDate":"2010-02-26", "attachments":[                ]} ], "2022":[{ "id":1244,              "eventName":"sample_02",              "categoryType":"CUSTOM_NOTES",              "tags":"tag1, tag12, tag3, tag52, tag19",              "startDate":"2022-10-04",              "endDate":"2022-12-12",              "attachments":[                              ]           },           {              "id":1245,              "eventName":"hello_03",              "categoryType":"CUSTOM_NOTES",              "tags":"tag1, tag12",              "startDate":"2022-06-01",              "endDate":"2010-06-26",              "attachments":[                              ]           }        ]     };
    const result = Object.values(obj).flat().reduce((a,e)=>{
        if(new Date(e.startDate)<new Date(a.min) || !a.min) a.min=e.startDate;
        if(new Date(e.startDate)>new Date(a.max)) a.max=e.startDate;
        return a;
    },{min:null, max:null});
    
    console.log(result);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2012-07-16
      • 2020-02-12
      • 1970-01-01
      相关资源
      最近更新 更多