帮我优化下 这段 js代码 我感觉写的好凌乱 variation(id, item, index) { console.log(index, ' index 124') console.log(id, ' 125') console.log(item, ' item') // 添加子元素 this.$api.mine.extendList({ id: id }).then((res) => { console.log(res.data.userExtendOne) if (this.treeData[index].child) { //push 1个人 if (res.data.userExtendOne && res.data.isLeader == 0) { console.log('1111') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ) } //push 2个人 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.isLeader == 0) { console.log('2222') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ) } //push 3个人 console.log(res.data.isLeader, '150----') if (res.data.isLeader == 1) { console.log('3333') this.treeData[index].child.push({ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree }) } } else { //push 1 孩子的 孩子 if (res.data.userExtendOne) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, ]) } //push 2 孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, ]) } //push 3孩子的 孩子 if (res.data.userExtendOne && res.data.userExtendTwo && res.data.userExtendThree) { this.$set(this.treeData[index], 'child', [{ userExtendOne: res.data.userExtendOne, userOne: res.data.userOne }, { userExtendTwo: res.data.userExtendTwo, userTwo: res.data.userTwo }, { userExtendThree: res.data.userExtendThree, userThree: res.data.userThree } ]) } } // this.treeData.push(this.removeDuplicates(this.treeData)) console.log("===data===" + JSON.stringify(this.treeData)) }) },
时间: 2024-03-16 20:41:48 浏览: 68
js代码优化的小示例
可以尝试优化以下这段代码:
```
async variation(id, item, index) {
console.log(index, ' index 124');
console.log(id, ' 125');
console.log(item, ' item');
const res = await this.$api.mine.extendList({ id });
console.log(res.data.userExtendOne);
const child = [];
if (res.data.userExtendOne && res.data.isLeader === 0) {
console.log('1111');
child.push({
userExtendOne: res.data.userExtendOne,
userOne: res.data.userOne
});
}
if (res.data.userExtendOne && res.data.userExtendTwo && res.data.isLeader === 0) {
console.log('2222');
child.push({
userExtendOne: res.data.userExtendOne,
userOne: res.data.userOne
}, {
userExtendTwo: res.data.userExtendTwo,
userTwo: res.data.userTwo
});
}
console.log(res.data.isLeader, '150----');
if (res.data.isLeader === 1) {
console.log('3333');
child.push({
userExtendOne: res.data.userExtendOne,
userOne: res.data.userOne
}, {
userExtendTwo: res.data.userExtendTwo,
userTwo: res.data.userTwo
}, {
userExtendThree: res.data.userExtendThree,
userThree: res.data.userThree
});
}
if (child.length > 0) {
if (this.treeData[index].child) {
this.treeData[index].child.push(...child);
} else {
this.$set(this.treeData[index], 'child', child);
}
}
console.log("===data===" + JSON.stringify(this.treeData));
},
```
- 将异步请求改为使用 async/await;
- 简化了添加子元素的逻辑;
- 将添加子元素的重复代码提取到一个数组中,避免代码冗余;
- 在添加子元素前,先判断 child 数组是否为空,避免在 push 时出现问题。
阅读全文