【发布时间】:2020-03-26 17:47:17
【问题描述】:
以下是获取下拉列表中选项的 2 个函数。我想让它成为一个功能而不是两个不同的功能。基于 fromtype/totype 的选项很少。但最像 case A、I、W 等对于这两种功能都是通用的。如何将以下2个函数合为一个函数。
我在想一个函数
const getOptionKey = (type, metaType) => {
switch (type) {
case 'A':
type = ['A'];
break;
case 'W1':
case 'W':
type = ['W'];
break;
case 'I':
type = ['I'];
break;
case 'E':
switch (metaType) {
case 'A':
type = ['A'];
break;
case 'W2':
fromType = ['W'];
break;
default:
fromType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
type = [];
}
}
但是我无法想出更好的解决方案来对不常见的 switch 案例进行分类。
const getFromTypeOptionKey = (fromType, fromMetaType) => {
switch (fromType) {
case 'A':
fromType = ['A'];
break;
case 'I':
fromType = ['I'];
break;
case 'W1':
case 'W':
fromType = ['W'];
break;
case 'R':
fromType = ['R'];
break;
case 'E':
switch (fromMetaType) {
case 'A1':
fromType = ['A'];
break;
case 'W2':
fromType = ['W'];
break;
case 'I1':
case 'L':
case 'CH':
case 'C1':
case 'AT':
fromType = ['C'];
break;
default:
fromType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
fromType = [];
}
return fromType;
};
const getToTypeOptionKey = (toType, toMetaType) => {
switch (toType) {
case 'A':
toType = ['A'];
break;
case 'CK':
toType = ['CK'];
break;
case 'I':
toType = ['I'];
break;
case 'W1':
case 'W':
toType = ['W'];
break;
case 'E':
switch (toMetaType) {
case 'A1':
toType = ['A'];
break;
case 'W2':
toType = ['W'];
break;
case 'I1':
case 'CW':
case 'C1':
case 'PD':
case 'PU':
case 'AT':
toType = ['C'];
break;
default:
toType = [
'A',
'W',
'C',
'I',
'CK'
];
}
break;
default:
toType = [];
}
return toType;
};
【问题讨论】:
-
案例“E”应该调用
getToTypeOptionKey,其toMetaType作为toType。 -
@Mr.Polywhirl 我已经编辑了我的问题。您能否检查一下我正在尝试的解决方案。
标签: javascript reactjs function switch-statement