优化代码 const {businessType, certType, certNo, longTude, uploadZqFlag} = this.defaults businessType.editDisplay = true businessType.viewDisplay = true certType.editDisplay = true certType.viewDisplay = true certNo.editDisplay = true certNo.viewDisplay = true longTude.editDisplay = true longTude.viewDisplay = true uploadZqFlag.viewDisplay = true
时间: 2024-04-13 14:30:21 浏览: 276
您可以使用对象解构和对象属性简写来优化这段代码。以下是优化后的代码示例:
```javascript
const { businessType, certType, certNo, longTude, uploadZqFlag } = this.defaults;
const displayProperties = ['editDisplay', 'viewDisplay'];
[businessType, certType, certNo, longTude].forEach((item) => {
displayProperties.forEach((prop) => {
item[prop] = true;
});
});
uploadZqFlag.viewDisplay = true;
```
通过将需要设置为 `true` 的属性名称存储在数组 `displayProperties` 中,您可以使用嵌套的循环遍历来设置对应的属性。这样,您可以用更简洁的方式实现相同的功能。
阅读全文