解析事件对象数组并将状态集与正确的开始停止日期匹配

# statusstart        statusend          statusname

2020-08-25 07:00:24 2020-08-25 07:00:24 MICRO STOP  

2020-08-25 07:00:24 2020-08-25 07:02:24 UNKNOWN STATUS

2020-08-25 07:02:24 2020-08-25 07:04:24 UNKNOWN STATUS

2020-08-25 07:04:24 2020-08-25 07:06:24 UNKNOWN STATUS

2020-08-25 07:06:24 2020-08-25 07:08:24 UNKNOWN STATUS

2020-08-25 07:08:24 2020-08-25 07:10:24 UNKNOWN STATUS

2020-08-25 07:10:24 2020-08-25 07:12:24 UNKNOWN STATUS

2020-08-25 07:12:24 2020-08-25 07:15:24 UNKNOWN STATUS

2020-08-25 07:15:24 2020-08-25 07:16:10 PRODUCTION

2020-08-25 07:16:10 2020-08-25 07:17:00 PRODUCTION

2020-08-25 07:17:00 2020-08-25 07:18:00 MICRO STOP  

2020-08-25 07:18:00 2020-08-25 07:19:00 UNKNOWN STATUS

我想输出这样的数组:


[['MICRO STOP', 2020-08-25 07:00:24, 2020-08-25 07:00:24],

['UNKNOWN STATUS', 2020-08-25 07:00:24, 2020-08-25 07:15:24],

['PRODUCTION', '2020-08-25 07:15:24', '2020-08-25 07:17:00'],

['MICRO STOP', '2020-08-25 07:17:00', '2020-08-25 07:18:00']

['UNKNOWN STATUS', '2020-08-25 07:18:00', '2020-08-25 07:19:00']

]

这是我试过的:


let status = '';

let statusStart: Date;

let statusEnd: Date;


let data = new Array<any>();


this.evolutionAnalysis.forEach((e, idx) => {

    if(status === '') {

        status = e.statusname;

        statusStart = new Date(e.statusstart);

    }


    if (e.statusname !== status) {

        

        statusEnd = new Date(this.evolutionAnalysis[idx - 1].statusend);

        data.push([

            this.operatorsAnalysis.find(o => o.operatorid == e.operatorid).operatorname, 

            status,

            statusStart,

            statusEnd

        ]);


        status = e.statusname;

    } 

});

在我知道 statusEnd 之后,如何确定 statusStart


三国纷争
浏览 126回答 2
2回答

汪汪一只猫

您可以检查前任,如果它具有相同的名称并更新结束时间。const&nbsp; &nbsp; 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' }],&nbsp; &nbsp; result = data.reduce((r, { statusstart, statusend, statusname }, i, a) => {&nbsp; &nbsp; &nbsp; &nbsp; if (a[i - 1]?.statusname === statusname) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r[r.length - 1][2] = statusend;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r.push([statusname, statusstart, statusend]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; }, []);console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

守着星空守着你

您可以按连续分组statusname并映射到该组,获取statusstart组的第一个元素和statusend最后一个元素的const evolutionAnalysis = [&nbsp; { statusstart: "2020-08-25 07:00:24", statusend: "2020-08-25 07:00:24", statusname:"MICRO STOP" },&nbsp; { statusstart: "2020-08-25 07:00:24", statusend: "2020-08-25 07:02:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:02:24", statusend: "2020-08-25 07:04:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:04:24", statusend: "2020-08-25 07:06:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:06:24", statusend: "2020-08-25 07:08:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:08:24", statusend: "2020-08-25 07:10:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:10:24", statusend: "2020-08-25 07:12:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:12:24", statusend: "2020-08-25 07:15:24", statusname:"UNKNOWN STATUS" },&nbsp; { statusstart: "2020-08-25 07:15:24", statusend: "2020-08-25 07:16:10", statusname:"PRODUCTION" },&nbsp; { statusstart: "2020-08-25 07:16:10", statusend: "2020-08-25 07:17:00", statusname:"PRODUCTION" },&nbsp; { statusstart: "2020-08-25 07:17:00", statusend: "2020-08-25 07:18:00", statusname:"MICRO STOP" },&nbsp; { 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++) {&nbsp; const group = [evolutionAnalysis[i]]&nbsp; while (&nbsp; &nbsp; evolutionAnalysis[i + 1] &&&nbsp; &nbsp; evolutionAnalysis[i + 1].statusname === evolutionAnalysis[i].statusname&nbsp; ) {&nbsp; &nbsp; group.push(evolutionAnalysis[i + 1])&nbsp; &nbsp; i++&nbsp; }&nbsp; groupedEvolutionAnalysis.push(group)}const res = groupedEvolutionAnalysis.map(group => [&nbsp; group[0].statusname,&nbsp; group[0].statusstart,&nbsp; group[group.length - 1].statusend])console.log(res)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript