问题
在维护一个老项目时发现map循环数组循坏到index为14时就自动不循环了(数组长度为79),并且也不运行后面的代码
代码
// 问题代码
await useMyFetch('url', {
afterFetch(ctx) {
const resdata = safeParse(ctx.data) // json字符串转换为对象
const data = resdata.result.items.map((v) => {
if (v.modelSettings !== null)
return { ...safeParse(v.modelSettings), id: v.id, icon: v.watchStatus ? watchTypeIconList.filter(x => x.name === v.watchType)[0].onLineIcon : watchTypeIconList.filter(x => x.name === v.watchType)[0].icon }
})
console.log(data);
deviceLabelData.value = data.filter(x => x !== undefined)
return ctx
},
}).post({
maxResultCount: 999,
skipCount: 0,
})
debugger发现当运行到resdata.result.items.map
函数中循环到 index为14时就会自动停止 后面的console等函数也没有输出。控制台没有报错。
当我注释掉icon属性后则正常运行。
await useMyFetch('url', {
afterFetch(ctx) {
const resdata = safeParse(ctx.data) // json字符串转换为对象
const data = resdata.result.items.map((v) => {
if (v.modelSettings !== null)
return {
...safeParse(v.modelSettings),
id: v.id,
// icon: v.watchStatus ? watchTypeIconList.filter(x => x.name === v.watchType)[0].onLineIcon : watchTypeIconList.filter(x => x.name === v.watchType)[0].icon 删除掉这条语句后就恢复正常了
}
})
console.log(data);
deviceLabelData.value = data.filter(x => x !== undefined)
return ctx
},
}).post({
maxResultCount: 999,
skipCount: 0,
})
// 正常运行
async function getVideoLabelData() {
await useMyFetch("url",{
afterFetch(ctx) {
const resdata = safeParse(ctx.data);
const data = resdata.result.items.map((v) => {
if (v.modelSettings !== null)
return {
...safeParse(v.modelSettings),
id: v.id,
videoUrl: v.videoAddress,
icon: v.status
? watchTypeIconList.filter((x) => x.name === "xxx")[0]
.onLineIcon
: watchTypeIconList.filter((x) => x.name === v.watchType)[0]
.icon,
};
});
videosLabelData.value = data.filter((x: any) => x !== undefined);
return ctx;
},
}
).get();
}
猜测是因为js语句的格式不规范问题导致的。(因为另一个函数getVideoLabelData
中跟这个函数几乎一模一样但却能正常运行)
修改后则运行正常
async function getDeviceLabelData() {
await useMyFetch("url", {
afterFetch(ctx) {
const resdata = safeParse(ctx.data);
let data = [];
for (let index = 0; index < resdata.result.items.length; index++) {
const v = resdata.result.items[index];
if (v.modelSettings !== null) {
let obj = watchTypeIconList.find((x) => x.name === v.watchType);
let temp = v.watchStatus ? obj?.onLineIcon : obj?.icon;
data.push({
...safeParse(v.modelSettings),
id: v.id,
icon: temp,
});
}
}
deviceLabelData.value = data.filter((x) => x !== undefined);
return ctx;
},
}).post({
maxResultCount: 999,
skipCount: 0,
});
标签:map,const,ctx,resdata,filter,循环,自动,data,icon
From: https://www.cnblogs.com/GoodMemoryBlog/p/17381642.html