From 268c56fb2605248b5fdaf093eb74346fdd1ce3a1 Mon Sep 17 00:00:00 2001 From: Brajesh Kumar Date: Mon, 9 Oct 2023 20:02:06 +0530 Subject: [PATCH] #2004 - refactor for codeclimate --- .../educational-material.component.ts | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/app/child-dev-project/children/educational-material/educational-material-component/educational-material.component.ts b/src/app/child-dev-project/children/educational-material/educational-material-component/educational-material.component.ts index f40407630d..08f9dc519b 100644 --- a/src/app/child-dev-project/children/educational-material/educational-material-component/educational-material.component.ts +++ b/src/app/child-dev-project/children/educational-material/educational-material-component/educational-material.component.ts @@ -100,31 +100,21 @@ export class EducationalMaterialComponent implements OnInit { this.records.forEach((m) => { const { materialType, materialAmount } = m; + const label = materialType?.label; - if (materialType.label) { - const label = materialType.label; - - if (!summary.has(label)) { - summary.set(label, { count: 0, sum: 0 }); - } - - const labelData = summary.get(label); - labelData.count++; - labelData.sum += materialAmount; + if (label) { + summary.set(label, (summary.get(label) || { count: 0, sum: 0 })); + summary.get(label)!.count++; + summary.get(label)!.sum += materialAmount; } }); - const summaryArray: string[] = Array.from(summary.entries()).map( - ([label, labelData]) => `${label}: ${labelData.sum}` - ); - - const avgSummaryArray: string[] = Array.from(summary.entries()).map( - ([label, labelData]) => { - const avg = parseFloat((labelData.sum / labelData.count).toFixed(2)); - average.set(label, avg); - return `${label}: ${avg}`; - } - ); + const summaryArray = Array.from(summary.entries(), ([label, { sum }]) => `${label}: ${sum}`); + const avgSummaryArray = Array.from(summary.entries(), ([label, { count, sum }]) => { + const avg = parseFloat((sum / count).toFixed(2)); + average.set(label, avg); + return `${label}: ${avg}`; + }); this.summary = summaryArray.join(", "); this.avgSummary = avgSummaryArray.join(", ");