for (let k in sysSetDis) { if (k.sysSetDis[k] === true) { arr.push(k) } console.log(k.sysSetDis[k]); }
时间: 2024-04-10 13:33:55 浏览: 11
这段代码是一个 for-in 循环,用于遍历对象 `sysSetDis` 中的属性。在循环体内部,我们首先检查属性值是否为 `true`,如果是,则将该属性名 `k` 添加到数组 `arr` 中。然后,我们打印出属性值。请注意,代码中的 `k.sysSetDis[k]` 是错误的,应该是 `sysSetDis[k]`。修正后的代码如下:
```javascript
for (let k in sysSetDis) {
if (sysSetDis[k] === true) {
arr.push(k);
}
console.log(sysSetDis[k]);
}
```
这段代码会遍历 `sysSetDis` 对象的所有属性,并将属性值为 `true` 的属性名添加到数组 `arr` 中。同时,会打印出每个属性的值。
相关问题
selectPeople(row) { // console.log(row, "根据身份证号查询成员"); this.title = "查看信息"; this.dialogSelectMember = true; selectByIdentityCard(row.value).then((res) => { this.tableDataPeople = res.data.data; console.log(this.tableDataPeople); this.arr.push(res.data.data[0].id); let newArr = []; let map = new Map(); for (let i = 0; i < this.arr.length; i++) { if (!map.has(this.arr[i])) { map.set(this.arr[i], true); newArr.push(this.arr[i]); } } this.form.memberId = newArr; console.log(this.form.memberId, "863"); }); // this.selectByIdentityCardFn(row); },重新选择参数 之前的参数怎么冲数组中删除 前端代码实现
可以使用JavaScript数组的`splice()`方法来删除数组中的元素。具体实现如下:
```javascript
selectPeople(row) {
this.title = "查看信息";
this.dialogSelectMember = true;
selectByIdentityCard(row.value).then((res) => {
this.tableDataPeople = res.data.data;
console.log(this.tableDataPeople);
this.arr.push(res.data.data[0].id);
let newArr = [];
let map = new Map();
for (let i = 0; i < this.arr.length; i++) {
if (!map.has(this.arr[i])) {
map.set(this.arr[i], true);
newArr.push(this.arr[i]);
}
}
this.form.memberId = newArr;
console.log(this.form.memberId, "863");
});
// 从数组中删除之前的参数
let index = this.form.memberId.indexOf(row.value);
if (index !== -1) {
this.form.memberId.splice(index, 1);
}
}
```
在`selectPeople()`函数中,在调用`selectByIdentityCard()`方法之前,先使用`indexOf()`方法查找数组中之前的参数在数组中的位置,如果找到了,则使用`splice()`方法删除该元素。这样就可以将之前的参数从数组中删除了。
function leaseSubmit() { let arr = [] house_label.value?.forEach(v => { if (v.isActive === true) { arr.push(v.value) } }) form.value.houseLabel = arr.join(",") let res = {} console.log(111, form.value); //去除头部地址 let imageUrl = [] form.value.picture?.split(",").forEach(v => imageUrl.push('/profile' + v.split('/profile')[1])) form.value.picture = imageUrl.join(",") proxy.$refs.uForm.validate(async (valid) => { if (valid) { if (form.value.id) { res = await uni.$u.api.neighbor.housingEdit(form.value) } else { res = await uni.$u.api.neighbor.housing(form.value) } if (res.success) { proxy.useToast({ title: "提交成功", }); setTimeout(() => { uni.navigateTo({ url: "/neighbor/houseRental/feedBack" }); }, 1000); } else { proxy.useToast({ icon: "error", title: "提交失败", }); } } }); } 优化
这段代码的功能不够清晰,缺少注释和变量命名不够规范,建议进行以下优化:
1. 增加注释说明每个部分的功能和作用;
2. 对变量进行规范的命名,尽量使用有意义的单词或者单词缩写;
3. 将重复的代码进行封装,减少代码冗余;
4. 将函数进行拆分,保证每个函数只处理一个任务;
5. 使用 async/await 简化异步调用。
以下是一个可能的优化示例:
```javascript
// 提交表单
async function leaseSubmit() {
// 获取房屋标签选中项的值
const selectedLabels = house_label.value?.filter(v => v.isActive).map(v => v.value) || []
form.value.houseLabel = selectedLabels.join(",")
// 去除图片地址中的前缀
const imageUrl = form.value.picture?.split(",").map(v => "/profile" + v.split("/profile")[1]) || []
form.value.picture = imageUrl.join(",")
// 验证表单
const valid = await proxy.$refs.uForm.validate()
if (!valid) {
return
}
const res = form.value.id
? await uni.$u.api.neighbor.housingEdit(form.value)
: await uni.$u.api.neighbor.housing(form.value)
if (res.success) {
proxy.useToast({ title: "提交成功" })
setTimeout(() => {
uni.navigateTo({ url: "/neighbor/houseRental/feedBack" })
}, 1000)
} else {
proxy.useToast({ icon: "error", title: "提交失败" })
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)