js如何获取数组对象中的最大时间最小时间?
例如
var array=[ {
"id": 119,
"departDate":"2022-12-11",
"arriveDate": "2022-12-15",
},
{
"index_id": 120,
"departDate":"2022-12-10",
"arriveDate": "2022-12-13",
},
{
"index_id": 121,
"departDate":"2022-12-9",
"arriveDate": "2022-12-20",
}
];
这个数组对象departDate 取最小值,arriveDate取最大值,最后 departDate最小值应为2022-12-9,arriveDate最大值为2022-12-20,而且考虑到ios不会识别2022-12-15格式而是2022/12/15,应该如何处理呢?
回答:
let mint = array.reduce((mint, item) => { let t = item.departDate
return new Date(mint.replace(/\-/g, '/')) > new Date(t.replace(/\-/g, '/')) ? t : mint
}, array[0].departDate)
let maxt = array.reduce((maxt, item) => {
let t = item.arriveDate
return new Date(maxt.replace(/\-/g, '/')) < new Date(t.replace(/\-/g, '/')) ? t : maxt
}, array[0].arriveDate)
回答:
const min = Math.min(...array.map(item => new Date(item.departDate)));const max = Math.max(...array.map(item => new Date(item.arriveDate)));
// const minDate = new Date(min);
// const maxDate = new Date(max);
以上是 js如何获取数组对象中的最大时间最小时间? 的全部内容, 来源链接: utcz.com/p/933411.html