deepseek帮我优化了一个写了3层for循环的代码,结果从3层for循环逐步优化为1层for循环,只想说遇到事情不要慌,感觉写的不好的代码可以让ai帮你进行优化,有时候会得到意想不到的效果。
首先是要对优化结果有一个预期的结果,我的预期是减少for循环次数,针对本地优化来看,最后超出了我的预期。
需求如下:按照如下格式生成一个需要合并哪些单元格的数组,按照单元格的维度,即:如果我有一个3行的数据,如果需要合并2行列,则生成后的数组如下:
{
"row": 0,
"col": 0,
"rowspan": 3,
"colspan": 1
},
{
"row": 0,
"col": 1,
"rowspan": 3,
"colspan": 1
},
{
"row": 1,
"col": 0,
"rowspan": 0,
"colspan": 1
},{
"row": 1,
"col": 2,
"rowspan": 0,
"colspan": 1
},{
"row": 2,
"col": 0,
"rowspan": 0,
"colspan": 1
},{
"row": 2,
"col": 1,
"rowspan": 0,
"colspan": 1
}
{ row: index * 2, col: 0, rowspan: 2, colspan: 1 }
我的代码如下:
// spanArr为一个二维数组例如[[0,1,2],[3,4,5],[6,7,8]],代表的含义为:将表格合并为3行,第0行、第1行、第2行合并为1行
mergeCells() {
if (this.isEditor) return []
// 计算合并行数
const spanArr = this.getSpanPosition(this.form.tableData, ['siteCode'])
// console.log('spanArr', spanArr)
const arr = []
spanArr.forEach((item, index) => {
// 判断基数对象相同合并单元格 返回格式 {"row": 0,"col": 0,"rowspan": 2,"colspan": 1}
const spanValue = spanArr[index]
const spanLength = spanArr[index].length
// 获取需要合并的行
for (let j = 0; j < spanValue.length; j++) {
// 判断为合并的开始行,需要设置rowspan为需要合并的行数,colspan为0不合并列
if (spanValue[j] % spanLength === 0) {
// 获取需要合并的列
for (let i = 0; i < 6; i++) {
// 合并1-5列
arr.push({
row: spanValue[j],
col: i,
rowspan: spanLength,
colspan: 1
})
}
} else {
for (let i = 0; i < 6; i++) {
// 合并1-5列
arr.push({
row: spanValue[j],
col: i,
rowspan: 0,
colspan: 1
})
}
}
}
})
// console.log(arr, 'arr')
return arr
} 帮我优化以上代码
第一次优化后效果,从结果来看比上面代码量更少,看起来也更清爽
mergeCells() {
if (this.isEditor) return [];
// 计算合并行数
const spanArr = this.getSpanPosition(this.form.tableData, ['siteCode']);
const mergedCells = [];
spanArr.forEach((spanGroup, groupIndex) => {
const groupLength = spanGroup.length;
spanGroup.forEach((rowIndex, rowPosition) => {
for (let colIndex = 0; colIndex < 6; colIndex++) {
const cell = {
row: rowIndex,
col: colIndex,
rowspan: rowPosition === 0 ? groupLength : 0, // 只有第一行设置 rowspan
colspan: 1
};
mergedCells.push(cell);
}
});
});
return mergedCells;
}
第二次优化后效果,从结果来看少了一层for循环,循环次数降到了m*n
mergeCells() {
if (this.isEditor) return [];
// 计算合并行数
const spanArr = this.getSpanPosition(this.form.tableData, ['siteCode']);
const mergedCells = [];
// 遍历每个分组
for (let groupIndex = 0; groupIndex < spanArr.length; groupIndex++) {
const spanGroup = spanArr[groupIndex];
const groupLength = spanGroup.length;
// 遍历分组中的每一行
for (let rowPosition = 0; rowPosition < groupLength; rowPosition++) {
const rowIndex = spanGroup[rowPosition];
// 直接生成 6 列的配置
mergedCells.push(
{ row: rowIndex, col: 0, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 },
{ row: rowIndex, col: 1, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 },
{ row: rowIndex, col: 2, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 },
{ row: rowIndex, col: 3, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 },
{ row: rowIndex, col: 4, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 },
{ row: rowIndex, col: 5, rowspan: rowPosition === 0 ? groupLength : 0, colspan: 1 }
);
}
}
return mergedCells;
}
第三次优化,由于合并的行数是固定的,因此不需要写getSpanPosition
这个方法来计算需要合并的行和列,只需要将数据的index%3
来判断是不是合并行的第一行数据即可,循环次有和第二次相比是相同的,但是从代码上看比第二次的更加少
mergeCells() {
if (this.isEditor) return []
// 计算合并行数
const mergedCells = []
// 判断基数对象相同合并单元格,每3行合并一下 返回格式 {"row": 0,"col": 0,"rowspan": 2,"colspan": 1}
this.form.tableData.forEach((item, rowIndex) => {
const rowspan = rowIndex % 3 === 0 ? 3 : 0
mergedCells.push(
{ row: rowIndex, col: 0, rowspan, colspan: 1 },
{ row: rowIndex, col: 1, rowspan, colspan: 1 },
{ row: rowIndex, col: 2, rowspan, colspan: 1 },
{ row: rowIndex, col: 3, rowspan, colspan: 1 },
{ row: rowIndex, col: 4, rowspan, colspan: 1 },
{ row: rowIndex, col: 5, rowspan, colspan: 1 }
)
})
console.log(mergedCells, 'mergedCells')
return mergedCells
}
其实用ai
写代码,不止可以帮我们写不会的,能写出来的代码再进行深入思考也是有一定优化空间的,学习一种代码思维也是很好的。