CRM前端表单中如何利用数组匹配选项集值完成评分权重计算?
CRM前端表单中如何利用数组匹配选项集值完成评分权重计算?
看起来你已经找对路子啦!用数组+对象来管理选项集和对应的分值/权重,确实比堆一堆零散变量要整洁太多,也更易维护。我来帮你把这段代码补全优化,顺便把面向对象的思路落地得更顺畅~
首先先给你点个赞,你用Array.find()来匹配选项集值对应分值的思路完全没问题!接下来我们只需要把权重的匹配逻辑也统一起来,再补上加权计算和平均分的步骤就行,还能封装个通用函数减少重复代码。
优化后的完整代码
function CalculateAverageScore(executionContext) { // 获取表单上下文 const formContext = executionContext.getFormContext(); // 封装通用匹配函数:根据选项集值,从映射数组中拿到对应的分值/权重 // 这样不管是评分还是权重,都能用同一个方法处理,避免重复写find逻辑 function getMatchedValue(targetValue, matchArray) { // 先判断目标值是否为空,避免无效查找 if (!targetValue) return null; // 找到匹配的项 const matchedItem = matchArray.find(item => item.value === targetValue); // 返回对应分值,没找到就返回null return matchedItem ? matchedItem.score : null; } // 定义评分和权重的映射数组(可以单独抽成全局变量,方便后续修改维护) const scoreMapping = [ { "value": 778180000, "score": 1 }, { "value": 778180001, "score": 2 }, { "value": 778180002, "score": 3 }, { "value": 778180003, "score": 4 }, { "value": 778180004, "score": 5 } ]; const weightMapping = [ { "value": 778180000, "score": 0.10 }, { "value": 778180001, "score": 0.20 }, { "value": 778180002, "score": 0.30 }, { "value": 778180003, "score": 0.40 }, { "value": 778180004, "score": 0.50 } ]; // 获取Section A的两个选项集字段值 const sectionARatingValue = formContext.getAttribute("dp_sectionamanagerrating").getValue(); const sectionAWeightValue = formContext.getAttribute("dp_sectionaquestionweighting").getValue(); // 通过通用函数获取对应的评分和权重 const sectionAScore = getMatchedValue(sectionARatingValue, scoreMapping); const sectionAWeight = getMatchedValue(sectionAWeightValue, weightMapping); // 计算Section A的加权得分(先做有效值判断,避免出现NaN) let sectionAWeightedScore = 0; if (sectionAScore !== null && sectionAWeight !== null) { sectionAWeightedScore = sectionAScore * sectionAWeight; alert(`Section A加权得分:${sectionAWeightedScore}`); } else { alert("请完成Section A的评分和权重选择哦"); return; // 没填的话直接终止函数,避免后续错误计算 } // -----------------如果有多个Section(比如B、C),直接复制下面的逻辑即可----------------- // const sectionBRatingValue = formContext.getAttribute("dp_sectionbmanagerrating").getValue(); // const sectionBWeightValue = formContext.getAttribute("dp_sectionbquestionweighting").getValue(); // const sectionBScore = getMatchedValue(sectionBRatingValue, scoreMapping); // const sectionBWeight = getMatchedValue(sectionBWeightValue, weightMapping); // let sectionBWeightedScore = 0; // if (sectionBScore !== null && sectionBWeight !== null) { // sectionBWeightedScore = sectionBScore * sectionBWeight; // } // 计算最终平均分 // 情况1:如果所有Section的权重总和是1,直接累加加权得分就是最终结果 const averageScore = sectionAWeightedScore; // 情况2:如果权重总和不是1,就用「加权得分总和 ÷ 权重总和」来计算平均分 // const averageScore = (sectionAWeightedScore + sectionBWeightedScore) / (sectionAWeight + sectionBWeight); // 把计算结果赋值到表单的平均分字段(替换成你实际的字段名) if (averageScore) { formContext.getAttribute("dp_averagescore").setValue(averageScore.toFixed(2)); // 保留两位小数更友好 } }
关键优化点说明
- 通用匹配函数:把重复的
find逻辑封装成getMatchedValue,以后不管新增多少个Section,都能直接复用,不用重复写匹配代码,符合DRY原则。 - 空值判断:提前检查选项集值是否为空,避免出现
undefined或者NaN的错误,同时给用户友好提示。 - 数据与逻辑分离:评分和权重的映射数组相当于配置项,以后要修改分值、权重或者新增选项,直接改数组就行,不用动业务逻辑部分,维护成本大大降低。
- 扩展性强:如果后续新增Section B、C,只需要复制对应的字段获取、匹配、计算代码块就行,非常方便。
这样改造之后,你的代码就更贴近面向对象的思路了——用对象来封装数据(每个映射项都是一个对象),用方法(getMatchedValue)来封装逻辑,整体结构清晰,也更容易维护。
备注:内容来源于stack exchange,提问作者BenDev21
相关产品推荐
相关产品推荐

