优化一段代码ncheckPersonnelstate-payload-n----let-userinfo_tmp-=-stateuserInfo;n----consolelogpayload-payload;n----const-userId-=-payloaduserId;n----const-orgkey-=-payloadorgKey;n----const-orgId-=-payloadorgId;n----userinfo_tmpforEachelement-index-=-n-----
checkPersonnel(state, payload) { const userId = payload.userId; const orgkey = payload.orgKey; const orgId = payload.orgId;
state.userInfo = state.userInfo.map(userinfo => { if (userinfo.userId === userId) { return { ...userinfo, checked: !userinfo.checked }; } return userinfo; });
this.commit("getUserInfoPersonnel"); this.commit("getUserInfoOrg");
if (orgId) { const userlistByOrg = state.userlistByOrg;
for (let org of userlistByOrg) {
if (org.orgkey === orgkey) {
for (let value of org.value) {
if (value.orgId === orgId) {
const flag = value.user.every(user => user.checked);
console.log("find it", value);
console.log(flag);
break;
}
}
break;
}
}
} },
Explanation:
- Removed the unnecessary console.log() statements.
- Used Array.map() instead of Array.forEach() while updating the state.
- Used object destructuring in for loop.
- Used Array.every() to check if every user is checked in a given organization.
原文地址: https://www.cveoy.top/t/topic/p1f 著作权归作者所有。请勿转载和采集!