我的过滤器有一个switch case语句。 但它让我感到复杂的问题39你可以帮我解决一下吗?
以下是我的示例代码。它还包括一些案例。
angular.forEach(docsClone, function(doc) {
var added = false;
angular.forEach($scope.filterOptions, function(opt) {
if (!added) {
switch (opt.AttributeId) {
case 'documentStatus':
if ((doc.documentStatus !== undefined && doc.documentStatus !== null && doc.documentStatus.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planStatus':
if ((doc.planStatus !== undefined && doc.planStatus.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planFamily':
if ((doc.planProductFamily !== undefined && doc.planProductFamily !== null && doc.planProductFamily.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planYear':
planYear(doc, opt.AttributeValue, filteredDocs, added);
break;
case 'documentType':
if ((doc.documentType !== undefined && doc.documentType !== null && doc.documentType.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'businessEntity':
if ((doc.businessEntity !== undefined && doc.businessEntity !== null && doc.businessEntity.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'productClass':
if ((doc.productClass !== undefined && doc.productClass !== null && doc.productClass !== null && doc.productClass.indexOf(opt.AttributeValue) !== -1) ||
(doc.planProductClass !== undefined && doc.planProductClass.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'productType':
if ((doc.productType !== undefined && doc.productType !== null && doc.productType.indexOf(opt.AttributeValue) !== -1) ||
(doc.planProductType !== undefined && doc.planProductType.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
}
答案 0 :(得分:0)
我发现你有很多共同的条件,为什么不分解成常见的东西:
angular.forEach(docsClone, function(doc) {
var added = false;
angular.forEach($scope.filterOptions, function(opt) {
if (!added) {
switch (opt.AttributeId) {
case 'planStatus':
planYear(doc, opt.AttributeValue, filteredDocs, added);
break;
case 'documentType':
case 'documentStatus':
case 'planStatus':
case 'planFamily':
if ((doc[opt.AttributeId] !== undefined && doc[opt.AttributeId] !== null && doc[opt.AttributeId].indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
}
}
});
}
您可以根据所执行的比较/操作来概括您的案例陈述。