不用for还是用while
时间: 2024-04-14 13:26:54 浏览: 15
如果您想使用 while 循环来实现逻辑的修改,可以尝试以下代码:
```javascript
findOverrideMapId = async (overrides: CaseOverrideInfo[]) => {
let mapIdTowardIndex = -1;
let index = 0;
while (index < overrides.length) {
const item = overrides[index];
if (item.missingMap) {
const batchMaps = await this.getMaps(1, index + 1); // 获取对应位置的地图项
const mapIdTowardId = batchMaps.items.find((ele) => ele?.name === item.mapId)?.id;
mapIdTowardIndex = batchMaps.items.findIndex((ele) => ele?.name === item.mapId);
item.overrideMapId = mapIdTowardId || "";
break; // 找到匹配项后立即停止循环
}
index++;
}
this.setState({ initialPage: Math.ceil(mapIdTowardIndex / (pageSize + 2)) });
return overrides;
};
```
这段代码使用 while 循环来遍历 overrides 数组,并在找到匹配项后立即停止循环。同时,使用一个变量 index 来追踪当前遍历的位置。当未找到匹配项时,会将 index 自增,直到遍历完全部元素或找到匹配项为止。修改后的逻辑保持了原有的序号正确性,并且能够在找到匹配项后立即返回。