3 回答

TA贡献1810条经验 获得超4个赞
这里有两种方法。第一个返回数组,其中项目仅与数组中的前一个对象进行比较。第二个将该项与数组中当前项之前的所有对象进行比较。
const data = [{
title: 'Title 1',
startTime: '2019-09-26T06:00:00+0100',
endTime: '2019-09-26T08:30:00+0100'
},
{
title: 'Title 2',
startTime: '2019-09-26T08:00:00+0100',
endTime: '2019-09-26T08:15:00+0100'
},
{
title: 'Title 3',
startTime: '2019-09-26T08:30:00+0100',
endTime: '2019-09-26T09:25:00+0100'
},
{
title: 'Title 4',
startTime: '2019-09-26T09:25:00+0100',
endTime: '2019-09-26T10:25:00+0100'
},
{
title: 'Title 5',
startTime: '2019-09-26T10:25:00+0100',
endTime: '2019-09-26T11:00:00+0100'
}
].map(item => {
item.startTime = new Date(item.startTime)
item.endTime = new Date(item.endTime)
return item
})
const greaterThanLast = data.filter((item, index) => {
const last = data[index - 1]
if (typeof last == 'undefined') return true
return item.endTime > last.endTime
})
const greaterThanAll = data.reduce((result, current) => {
const endTimeGreaterThanRest = result.every((item) => current.endTime > item.endTime)
if (endTimeGreaterThanRest) result.push(current)
return result
}, [])
console.log(greaterThanLast)
console.log(greaterThanAll)

TA贡献1875条经验 获得超5个赞
假设您不想更改原始数组并假设您想删除“最低”值,您可以执行单个 for 循环:
跟踪以前的值。
将前一个值与当前值进行比较。
根据您的需要过滤该值。
关于代码的注释直接在下面,这个解决方案意味着只循环一次原始数组,利用单个 for-of 循环。
const input = [
{
title: 'Title 1',
startTime: '2019-09-26T06:00:00+0100',
endTime: '2019-09-26T08:30:00+0100'
},
{
title: 'Title 2',
startTime: '2019-09-26T08:00:00+0100',
endTime: '2019-09-26T08:15:00+0100'
},
{
title: 'Title 3',
startTime: '2019-09-26T08:30:00+0100',
endTime: '2019-09-26T09:25:00+0100'
},
{
title: 'Title 4',
startTime: '2019-09-26T09:25:00+0100',
endTime: '2019-09-26T10:25:00+0100'
},
{
title: 'Title 5',
startTime: '2019-09-26T10:25:00+0100',
endTime: '2019-09-26T11:00:00+0100'
}
];
function fixChronologicalItems(arr) {
// Keep track of the previous item.
let res = [], previous;
// Iterate all the items of the array.
for (let i = 0; i < arr.length; i++) {
// assume the current item is the looped one.
let item = arr[i];
// if our accumulator is not empty, acquire its last element considering it the previous item.
if (res[res.length - 1]) previous = res[res.length - 1];
else previous = arr[i], item = arr[i+1], i++; // if it doesn't, consider the current item the previous one, and the current item the next one, so increase the index by one to properly skip the next item.
// Acquire both datetimes.
let [previousDate, nextDate] = [new Date(previous.endTime), new Date(item.endTime)];
// if the previous item's date is before the next one, both items should be kept.
if (previousDate < nextDate) {
res.push(item); // <-- this will become the next "previous".
}
else res.push(previous); // <-- Otherwise, only the greatest date (which is the previous one) should be kept.
}
// finally, return the accumulator.
return res;
}
const res = fixChronologicalItems(input);
console.log(res);

TA贡献1887条经验 获得超5个赞
James Coyle 的回答比我的要流畅得多,但这是我对事情的看法。
在我的解决方案中,我检查下一个值是否小于当前值,如果是,则使用delete从数组中删除它。
如果我们得到数组中的最后一个值,那么我们就不再需要检查下一项,因此我们不进入 if 语句并中断循环。
var dataset = [{
title: 'Title 1',
startTime: '2019-09-26T06:00:00+0100',
endTime: '2019-09-26T08:30:00+0100'
},
{
title: 'Title 2',
startTime: '2019-09-26T08:00:00+0100',
endTime: '2019-09-26T08:15:00+0100'
},
{
title: 'Title 3',
startTime: '2019-09-26T08:30:00+0100',
endTime: '2019-09-26T09:25:00+0100'
},
{
title: 'Title 4',
startTime: '2019-09-26T09:25:00+0100',
endTime: '2019-09-26T10:25:00+0100'
},
{
title: 'Title 5',
startTime: '2019-09-26T10:25:00+0100',
endTime: '2019-09-26T11:00:00+0100'
}
]
var lengthOfArray = dataset.length;
for (i = 0; i < dataset.length; i++) {
if (lengthOfArray !== i + 1) {
var dt = new Date(dataset[i].endTime),
dt2 = new Date(dataset[i + 1].endTime);
if (dt2 < dt) delete dataset[i + 1].endTime;
}
else {
break;
}
}
console.log(dataset);
添加回答
举报