可以优化这段代码如下:

this.systemArray = [];
this.spotTradeArray = [];
this.sampleTradeArray = [];
this.biddingTradeArray = [];
let systemMsgCount = 0;
let systemSmsCount = 0;

for (const item of res.retData) {
  this.setSubscriptionStatus(item);
  
switch (item.suContentType) {
    case '1': // 系统
      if (item.supportInnerMsg == 1) {
        systemMsgCount++;
      }
      if (item.supportSms == 1) {
        systemSmsCount++;
      }
      this.systemArray.push(item);
      break;
    case '2': // spotTradeArray
      this.spotTradeArray.push(item);
      break;
    case '3': // sampleTradeArray
      this.sampleTradeArray.push(item);
      break;
    case '4': // biddingTradeArray
      this.biddingTradeArray.push(item);
      break;
    default:
      break;
  }
}

this.systemMsg += systemMsgCount;
this.systemSms += systemSmsCount;

这样优化后的代码将累加系统消息和系统短信的数量,避免了多次循环数组来计算数量,提高了代码的效率。

JavaScript 代码优化:高效处理系统消息和短信计数

原文地址: https://www.cveoy.top/t/topic/qksJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录