数据清洗:按顺序给固定长度的list插入空数据(补全)

设备面板有12个插槽,服务器返回的插槽数据不完整,每张卡有6个灯,服务器返回的灯数据也不完整。但界面要求必须显示12个插槽和每张卡6个灯,因此需要补全缺失的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:

设备面板有12个插槽:但是服务器返回来的数据是不完整的(有插板才会有数据)

每张卡有6个灯:同理,服务器返回来的数据也是不完整的(有插卡才有灯)

界面上的需要是:必须要12个插槽,每张卡得有6个灯。所以就要把缺失的给补全。代码如下:

this.detailInfoList.map((e, k) => {
          while (e.blade > this.devicesMsg.length && this.devicesMsg.length < 12) {
            if (e.blade === this.devicesMsg.length + 1) {
              e.isblank = 0
              const cardlist = e.cardlist
              if (cardlist.length === 6) {
                this.devicesMsg.push(e)
              } else {
                const cardlistNew = []
                cardlist.map((e2, k2) => {
                  while (e2.card > cardlistNew.length && cardlistNew.length < 6) {
                    if (e2.card === cardlistNew.length + 1) {
                      cardlistNew.push(e2)
                      break
                    } else {
                      cardlistNew.push({
                        id: 'null',
                        card: cardlistNew.length + 1
                      })
                    }
                  }

                  if (k2 === cardlistNew.length - 1 && cardlistNew.length !== 6) {
                    while (cardlistNew.length !== 6) {
                      this.devicesMsg.push({
                        id: 'null',
                        card: cardlistNew.length + 1
                      })
                    }
                  }
                })
              }
              break
            } else {
              this.devicesMsg.push({
                blade: this.devicesMsg.length + 1,
                cardlist: [],
                isblank: 1
              })
            }
          }
          // 最后一张卡
          if (k === this.detailInfoList.length - 1 && this.devicesMsg.length !== 12) {
            while (this.devicesMsg.length !== 12) {
              this.devicesMsg.push({
                blade: this.devicesMsg.length + 1,
                cardlist: [],
                isblank: 1
              })
            }
          }
        })

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值