优化这段代码that.Swiperlist = [] if (res.data.productInfo.videoUrl) { let videoObj = { type: 'video', // topTip: '顶部提示', currentTime: 0, //初始帧时间---默认缓存存储 poster: '', src: '', // bottomTip: '底部提示', } videoObj.src = that.particuInfo.videoUrl videoObj.poster = that.particuInfo.imgUrls[0] that.Swiperlist.push(videoObj) } else { // that.Swiperlist[0] = { // type: 'image', // src: that.particuInfo.imgUrls[0] // } that.swiperType = 'circle' } if (that.particuInfo.imgUrls) { let arr = that.particuInfo.imgUrls arr.forEach(v => { let obj = { type: 'image', src: v } that.Swiperlist.push(obj) }) }
时间: 2024-02-16 08:03:18 浏览: 122
MoveButton.rar
5星 · 资源好评率100%
可以将代码进行优化,避免多次访问that.particuInfo,同时使用数组的concat方法来合并数组,减少push操作。代码如下:
```
let particuInfo = res.data.productInfo;
let Swiperlist = [];
if (particuInfo.videoUrl) {
let videoObj = {
type: 'video',
currentTime: 0,
poster: particuInfo.imgUrls[0],
src: particuInfo.videoUrl,
};
Swiperlist.push(videoObj);
} else {
Swiperlist.push({
type: 'image',
src: particuInfo.imgUrls[0],
});
that.swiperType = 'circle';
}
if (particuInfo.imgUrls) {
let imgArr = particuInfo.imgUrls.map((v) => {
return {
type: 'image',
src: v,
};
});
Swiperlist = Swiperlist.concat(imgArr);
}
that.Swiperlist = Swiperlist;
```
阅读全文