我有開始和結束時間的對象的列表:排序開始時間,打破結束時間關係
let times = [
{start: moment().add(1, 'days'), end: moment().add(2, 'days')},
{start: moment().add(1, 'days'), end: moment().add(2, 'days')},
{start: moment().add(4, 'days'), end: moment().add(5, 'days')},
{start: moment().add(1, 'days'), end: moment().add(7, 'days')},
{start: moment().add(2, 'days'), end: moment().add(3, 'days')},
]
我想這些時間由開始時間排序(最早到最晚),同時打破與結束時間的關係(首先是較短的結束時間)。
所以結果應該是這樣的:
let sortedTimes = [
{start: moment().add(1, 'days'), end: moment().add(2, 'days')},
{start: moment().add(1, 'days'), end: moment().add(2, 'days')},
{start: moment().add(1, 'days'), end: moment().add(7, 'days')},
{start: moment().add(2, 'days'), end: moment().add(3, 'days')},
{start: moment().add(4, 'days'), end: moment().add(5, 'days')},
]
是否有一個首選的JavaScript的方式與高階函數/最小的語法來做到這一點?我開始寫一個腳本,但邏輯包含很多if - else if - else
語法,想知道是否有更好的方法。再次感謝!