对象去重累加errorCount时保留errorImpact字段的实现咨询
问题:按errorType去重并累加errorCount时保留errorImpact字段
我有一个名为indiErr的数组,每个元素包含errorType、errorImpact、errorCount字段。现有代码按errorType去重并累加errorCount时,会丢失errorImpact字段,仅保留errorType和累加后的errorCount。需要修改代码,让去重后的结果同时保留对应的errorImpact字段。
输入数组indiErr
[ { errorType: 'color-contrast', errorImpact: 'serious', errorCount: 8 }, { errorType: 'heading-order', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'image-alt', errorImpact: 'critical', errorCount: 1 }, { errorType: 'image-redundant-alt', errorImpact: 'minor', errorCount: 1}, { errorType: 'landmark-no-duplicate-contentinfo', errorImpact: 'moderate', errorCount: 1}, { errorType: 'landmark-unique', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'link-name', errorImpact: 'serious', errorCount: 7 }, { errorType: 'meta-viewport', errorImpact: 'critical', errorCount: 1 }, { errorType: 'region', errorImpact: 'moderate', errorCount: 30 }, { errorType: 'tabindex', errorImpact: 'serious', errorCount: 18 }, { errorType: 'color-contrast', errorImpact: 'serious', errorCount: 28 }, { errorType: 'landmark-one-main', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'page-has-heading-one', errorImpact: 'moderate', errorCount: 1 } ]
现有去重代码
const holder = {}; indiErr.forEach(function(d) { if (holder.hasOwnProperty(d.errorType)) { holder[d.errorType] = holder[d.errorType] + d.errorCount; // d.errorImpact= d.errorImpact // holder[d.errorImpact] = d.errorImpact } else { holder[d.errorType] = d.errorCount; // d.errorImpact= d.errorImpact // holder[d.errorImpact] = d.errorImpact } }); console.log('holder:', holder) const obj2 = []; for (const prop in holder) { obj2.push({ errorType: prop, errorCount: holder[prop] }); } console.log('Error Object:', obj2);
现有输出结果
Error Object: [ { errorType: 'color-contrast', errorCount: 36 }, { errorType: 'heading-order', errorCount: 1 }, { errorType: 'image-alt', errorCount: 1 }, { errorType: 'image-redundant-alt', errorCount: 1 }, { errorType: 'landmark-no-duplicate-contentinfo', errorCount: 1 }, { errorType: 'landmark-unique', errorCount: 1 }, { errorType: 'link-name', errorCount: 7 }, { errorType: 'meta-viewport', errorCount: 1 }, { errorType: 'region', errorCount: 30 }, { errorType: 'tabindex', errorCount: 18 }, { errorType: 'landmark-one-main', errorCount: 1 }, { errorType: 'page-has-heading-one', errorCount: 1 } ]
期望输出结果
[ { errorType: 'color-contrast', errorImpact: 'serious', errorCount: 36 }, { errorType: 'heading-order', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'image-alt', errorImpact: 'critical', errorCount: 1 }, { errorType: 'image-redundant-alt', errorImpact: 'minor', errorCount: 1}, { errorType: 'landmark-no-duplicate-contentinfo', errorImpact: 'moderate', errorCount: 1}, { errorType: 'landmark-unique', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'link-name', errorImpact: 'serious', errorCount: 7 }, { errorType: 'meta-viewport', errorImpact: 'critical', errorCount: 1 }, { errorType: 'region', errorImpact: 'moderate', errorCount: 30 }, { errorType: 'tabindex', errorImpact: 'serious', errorCount: 18 }, { errorType: 'landmark-one-main', errorImpact: 'moderate', errorCount: 1 }, { errorType: 'page-has-heading-one', errorImpact: 'moderate', errorCount: 1 } ]
修改后的代码
核心思路是让holder存储完整的对象(包含errorImpact和累加的errorCount),而非仅存储数字:
const holder = {}; indiErr.forEach(function(d) { if (holder.hasOwnProperty(d.errorType)) { // 已有该errorType,累加errorCount holder[d.errorType].errorCount += d.errorCount; } else { // 首次出现,存储包含完整字段的对象 holder[d.errorType] = { errorType: d.errorType, errorImpact: d.errorImpact, errorCount: d.errorCount }; } }); // 将holder中的值转为目标数组 const obj2 = Object.values(holder); console.log('Error Object:', obj2);
代码说明
- 遍历数组时,若
holder中已有对应errorType的条目,仅累加errorCount; - 首次遇到新的
errorType时,将包含errorImpact的完整对象存入holder; - 最后用
Object.values(holder)直接提取所有整理后的对象,转为数组即可。
注:此处默认同一
errorType对应的errorImpact值完全一致,若存在同一errorType对应不同errorImpact的场景,需额外添加冲突处理逻辑(比如保留首次出现的值、提示冲突等)。
内容的提问来源于stack exchange,提问作者Jan Willem
相关产品推荐
相关产品推荐

