findOverrideMapId = async (overrides: CaseOverrideInfo[]) => { const allMapsList = []; let page = 1; const pageSize = 40; while (page) { const batchMaps = await this.getMaps(page, pageSize); allMapsList.push(...batchMaps.items); page++; if (!batchMaps || batchMaps.items.length < pageSize) { break; } } let mapIdTowardIndex = -1; for (const item of overrides) { if (item.missingMap) { const mapIdTowardId = allMapsList.find((ele) => ele?.name === item.mapId)?.id; mapIdTowardIndex = allMapsList.findIndex((ele) => ele?.name === item.mapId); item.overrideMapId = mapIdTowardId || ""; } } this.setState({ initialPage: Math.ceil(mapIdTowardIndex / (pageSize + 2)) }); return overrides; }; 我想改一下逻辑,不用获取所有列表allMapsList。接口一找到就返回并且停止,并且还有序号需要正确
时间: 2024-02-14 15:28:19 浏览: 82
您想改变逻辑,不再获取所有的列表allMapsList,并且在找到匹配项后立即返回,并且确保序号正确。以下是您修改后的代码:
```javascript
findOverrideMapId = async (overrides: CaseOverrideInfo[]) => {
let mapIdTowardIndex = -1;
for (const [index, item] of overrides.entries()) {
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; // 找到匹配项后立即停止循环
}
}
this.setState({ initialPage: Math.ceil(mapIdTowardIndex / (pageSize + 2)) });
return overrides;
};
```
这样修改后的代码会在找到第一个匹配项后立即停止循环,并且使用正确的序号进行处理。注意,我假设您的`getMaps`方法会根据提供的页码和页面大小返回对应位置的地图项。
阅读全文