1. 使用 Array.reduce() 方法替换 map() 方法,以减少循环次数和内存占用。
  2. 避免在循环中频繁创建新数组,可以考虑使用 push() 方法将元素添加到现有数组中。
  3. 简化代码结构,避免嵌套过多的逻辑。 以下是一种可能的优化方案:
const arr = data.reduce((result: any[], item: any, index2: number) => {
  const len = item?.diVoList?.length;
  item.diVoList?.forEach((ite: any, index: number) => {
    result.push({
      serial: index2 + 1,
      projectType: item.projectType,
      projectId: item.projectId,
      projectName: item.projectName,
      targetDi: item.targetDi,
      actualDi: item.actualDi,
      spm: ite.spm,
      department: ite.department,
      targetDis: ite.targetDi,
      actualDis: ite.actualDi,
      span: index === 0 ? len : 0,
    });
  });
  return result;
}, []);

const tableData = arr.map((item: any, index: number) => {
  return { ...item, key: index };
});
``
datamapitem any index2 number = const len = itemdiVoListlength; itemdiVoListmapite any index number = arr = arr serial index2 + 1

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

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