2 回答
TA贡献1898条经验 获得超8个赞
您可以检查前任,如果它具有相同的名称并更新结束时间。
const
data = [{ statusstart: '2020-08-25 07:00:24', statusend: '2020-08-25 07:00:24', statusname: 'MICRO STOP' }, { statusstart: '2020-08-25 07:00:24', statusend: '2020-08-25 07:02:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:02:24', statusend: '2020-08-25 07:04:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:04:24', statusend: '2020-08-25 07:06:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:06:24', statusend: '2020-08-25 07:08:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:08:24', statusend: '2020-08-25 07:10:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:10:24', statusend: '2020-08-25 07:12:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:12:24', statusend: '2020-08-25 07:15:24', statusname: 'UNKNOWN STATUS' }, { statusstart: '2020-08-25 07:15:24', statusend: '2020-08-25 07:16:10', statusname: 'PRODUCTION' }, { statusstart: '2020-08-25 07:16:10', statusend: '2020-08-25 07:17:00', statusname: 'PRODUCTION' }, { statusstart: '2020-08-25 07:17:00', statusend: '2020-08-25 07:18:00', statusname: 'MICRO STOP' }, { statusstart: '2020-08-25 07:18:00', statusend: '2020-08-25 07:19:00', statusname: 'UNKNOWN STATUS' }],
result = data.reduce((r, { statusstart, statusend, statusname }, i, a) => {
if (a[i - 1]?.statusname === statusname) {
r[r.length - 1][2] = statusend;
} else {
r.push([statusname, statusstart, statusend]);
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
TA贡献1799条经验 获得超8个赞
您可以按连续分组statusname并映射到该组,获取statusstart组的第一个元素和statusend最后一个元素的
const evolutionAnalysis = [
{ statusstart: "2020-08-25 07:00:24", statusend: "2020-08-25 07:00:24", statusname:"MICRO STOP" },
{ statusstart: "2020-08-25 07:00:24", statusend: "2020-08-25 07:02:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:02:24", statusend: "2020-08-25 07:04:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:04:24", statusend: "2020-08-25 07:06:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:06:24", statusend: "2020-08-25 07:08:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:08:24", statusend: "2020-08-25 07:10:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:10:24", statusend: "2020-08-25 07:12:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:12:24", statusend: "2020-08-25 07:15:24", statusname:"UNKNOWN STATUS" },
{ statusstart: "2020-08-25 07:15:24", statusend: "2020-08-25 07:16:10", statusname:"PRODUCTION" },
{ statusstart: "2020-08-25 07:16:10", statusend: "2020-08-25 07:17:00", statusname:"PRODUCTION" },
{ statusstart: "2020-08-25 07:17:00", statusend: "2020-08-25 07:18:00", statusname:"MICRO STOP" },
{ statusstart: "2020-08-25 07:18:00", statusend: "2020-08-25 07:19:00", statusname:"UNKNOWN STATUS" },
]
const groupedEvolutionAnalysis = []
for (let i = 0; i < evolutionAnalysis.length; i++) {
const group = [evolutionAnalysis[i]]
while (
evolutionAnalysis[i + 1] &&
evolutionAnalysis[i + 1].statusname === evolutionAnalysis[i].statusname
) {
group.push(evolutionAnalysis[i + 1])
i++
}
groupedEvolutionAnalysis.push(group)
}
const res = groupedEvolutionAnalysis.map(group => [
group[0].statusname,
group[0].statusstart,
group[group.length - 1].statusend
])
console.log(res)
添加回答
举报