已知:有list这样一段时间,如何将最大时间值取出并且去除被包含的项,且如不包含则往内部追加,构成一个新数组
被替换前:
let list = [
{
start: "08:30:00",
end: "09:30:00",
},
{
start: "09:30:00",
end: "10:30:00",
},
{
start: "07:30:00",
end: "09:26:00",
},
{
start: "12:30:00",
end: "14:30:00",
},
];
替换后:
let list = [
{
start: "07:30:00",
end: "10:30:00",
},
{
start: "12:30:00",
end: "14:30:00",
},
];
筛选的函数:
function getNewTime(list){
let newData = [];
list.forEach((item, i) => {
let saveTime=[],
saveHas = []
newData.forEach(kon=>{
if(!(item.end<kon.start || item.start>kon.end)){
saveTime=saveTime.concat([kon.start,kon.end])
saveHas.push(kon)
}
})
saveTime=saveTime.concat([item.start,item.end]).sort()
newData=newData.filter(item=>{
return saveHas.indexOf(item) == -1
})
newData.push({
start: saveTime[0],
end: saveTime[saveTime.length-1]
})
});
console.log('newData',newData)
return newData
}
网友评论