为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

三国纷争 2023-03-03 10:30:56
# statusstart        statusend          statusname2020-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 STATUS2020-08-25 07:02:24 2020-08-25 07:04:24 UNKNOWN STATUS2020-08-25 07:04:24 2020-08-25 07:06:24 UNKNOWN STATUS2020-08-25 07:06:24 2020-08-25 07:08:24 UNKNOWN STATUS2020-08-25 07:08:24 2020-08-25 07:10:24 UNKNOWN STATUS2020-08-25 07:10:24 2020-08-25 07:12:24 UNKNOWN STATUS2020-08-25 07:12:24 2020-08-25 07:15:24 UNKNOWN STATUS2020-08-25 07:15:24 2020-08-25 07:16:10 PRODUCTION2020-08-25 07:16:10 2020-08-25 07:17:00 PRODUCTION2020-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
查看完整描述

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; }


查看完整回答
反对 回复 2023-03-03
?
守着星空守着你

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)


查看完整回答
反对 回复 2023-03-03
  • 2 回答
  • 0 关注
  • 125 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信