我有开始和结束时间的对象的列表:排序开始时间,打破结束时间关系
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
语法,想知道是否有更好的方法。再次感谢!