Home > other >  Deduplicate items in object - lost data
Deduplicate items in object - lost data

Time:11-06

I have this as an object (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
  }
]

With the following code I deduplicate items and sum the errorCount:

 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);

In the deduplication the values for 'errorImpact' get lost. This is the result:

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 }
]

(color-contrast is deduplicated, and errorCount is summed to: 36)

Is there any way to change the forEach function to also push 'errorImpact' value to 'obj2'? Have tried several things, but am stuck. Also don't know where to search for to solve this. Hope you have some pointers. Tnx!

I would like to have this as result:

[
  { 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
  }
]

CodePudding user response:

this did the trick:

const holder = {};

 indiErr.forEach(function(d) {
     if (!holder.hasOwnProperty(d.errorType)) {
         holder[d.errorType] = {errorImpact: d.errorImpact, errorCount: 0};
        }
    holder[d.errorType].errorCount =d.errorCount
 });

console.log('holder:', holder)
const obj2 = [];

for (const prop in holder) {
  obj2.push({ errorType: prop, ...holder[prop] });
 }

console.log('Error Object:', obj2);

CodePudding user response:

const errors = [
  { 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
  }
];

let r = {};
errors.forEach(({errorType,errorImpact,errorCount})=>
  (r[errorType]??= {errorType, errorImpact, errorCount:0}).errorCount  = errorCount);
console.log(Object.values(r));

  • Related