优化这段代码 let findNoAreasData = []; console.log('findNoAreas res', res); res.datas.data.forEach(x => { // 存在限购地区 if (x.no_areas.length > 0) { x.no_areas.filter(y => { if (y.province_id == this.addrCode.province) { this.goodsDatas.filter(z => { if (z.goods_sku.third_sku_id == y.sku_id) { z.goods_sku.notDelivery = true; if (z.goods_sku.notDelivery) { this.no_areas = true; } } }) } }) } });
时间: 2024-02-24 07:54:01 浏览: 26
这段代码可以优化如下:
```
let noAreasSkuIds = res.datas.data
.filter(x => x.no_areas.some(y => y.province_id == this.addrCode.province))
.map(x => x.no_areas.filter(y => y.province_id == this.addrCode.province))
.flat()
.map(y => y.sku_id);
if (noAreasSkuIds.length > 0) {
this.goodsDatas.forEach(z => {
if (noAreasSkuIds.includes(z.goods_sku.third_sku_id)) {
z.goods_sku.notDelivery = true;
this.no_areas = true;
}
});
}
```
这段代码首先使用 `filter` 函数筛选出限购地区中包含当前省份的数据,并使用 `map` 函数提取出对应的 `sku_id` 数组。然后判断该数组是否为空,如果不为空则遍历商品数据,将 `sku_id` 在数组中出现过的商品标记为不可配送。这样可以减少嵌套循环的层数,提高代码的可读性和执行效率。
相关问题
findTravelGuideList().then(res => { this.tableData = res.data.datas.ulist.map(de => { let tempList = de.imgLink.split(','); // 将图片分隔开来,分别存储在临时变量中 console.log(tempList); return de; }) this.total = res.data.datas.pageInfo.total; })怎么将请求的多少数组数据遍历到页面上vue
你可以使用 `v-for` 指令在 Vue 模板中遍历 `tableData` 数组,然后使用插值语法 `{{}}` 将每个数组元素的属性值显示出来。例如:
```html
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tableData" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
```
这里使用了 `v-for` 指令来遍历 `tableData` 数组,并使用 `:key` 绑定 `item.id` 作为每个元素的唯一标识。在每个 `<td>` 元素中,使用插值语法 `{{}}` 显示每个数组元素的属性值。
let params = Object.assign({}, datas.researchInfoForm)
这段代码是用来复制`datas.researchInfoForm`对象的属性到新的对象`params`中。`Object.assign()`方法用于将一个或多个源对象的属性复制到目标对象中,并返回目标对象。在这里,`params`对象将拥有与`datas.researchInfoForm`相同的属性和值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)