hello i would like to create a map based on these if.. is there a way to create a map and write better code?
findViewDetailPolicy(policy) {
if (policy['displayInfo']['displayTypeDetail']['displayType'] === 'STANDARD') {
this.path = this.policyPathDetailStandard;
this.router.navigate([this.path]);
}
if (policy['displayInfo']['displayTypeDetail']['displayType'] === 'PACCHETTO') {
this.path = this.policyPathDetailModulare;
this.router.navigate([this.path]);
}
if (policy['displayInfo']['displayTypeDetail']['displayType'] === 'SAVING') {
this.path = this.policyPathDetailSaving;
this.router.navigate([this.path]);
}
if (policy['displayInfo']['displayTypeDetail']['displayType'] === 'CQ') {
this.path = this.policyPathDetailCq;
this.router.navigate([this.path]);
}
if (policy['displayInfo']['displayTypeDetail']['displayType'] === 'COLLETTIVA') {
this.path = this.policyPathDetailCollective;
this.router.navigate([this.path]);
}
}
Your code can be boiled down to:
findViewDetailPolicy(policy) {
const map = { STANDARD: this.policyPathDetailStandard, ... };
this.path = map[policy['displayInfo']['displayTypeDetail']['displayType']];
this.router.navigate([this.path]);
}
The map could be like this:
const mapToPathProp = {
STANDARD: "policyPathDetailStandard",
PACCHETTO: "policyPathDetailModulare",
SAVING: "policyPathDetailSaving",
CQ: "policyPathDetailCq",
COLLETTIVA: "policyPathDetailCollective"
};
And then:
findViewDetailPolicy(policy) {
let pathProp = mapToPathProp[policy.displayInfo.displayTypeDetail.displayType];
if (pathProp) {
this.path = this[pathProp];
this.router.navigate([this.path]);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.