Home > other >  how to simplify this function
how to simplify this function

Time:10-13

I created a function that should return an array of numbers. it works, the only problem is that is really ugly and I was wonder if you know a better way to do it cause I can't.

totalMonth() {
    let arrSubKpiJan = [];
    let arrSubKpiFeb = [];
    let arrSubKpiMar = [];
    let arrSubKpiApr = [];
    let arrSubKpiMay = [];
    let arrSubKpiJun = [];
    let arrSubKpiJul = [];
    let arrSubKpiAug = [];
    let arrSubKpiSep = [];
    let arrSubKpiOct = [];
    let arrSubKpiNov = [];
    let arrSubKpiDec = [];

    for(let subkpi of this.overview) {
      arrSubKpiJan.push(subkpi.jan)
      arrSubKpiFeb.push(subkpi.feb)
      arrSubKpiMar.push(subkpi.mar)
      arrSubKpiApr.push(subkpi.apr)
      arrSubKpiMay.push(subkpi.may)
      arrSubKpiJun.push(subkpi.jun)
      arrSubKpiJul.push(subkpi.jul)
      arrSubKpiAug.push(subkpi.aug)
      arrSubKpiSep.push(subkpi.sep)
      arrSubKpiOct.push(subkpi.oct)
      arrSubKpiNov.push(subkpi.nov)
      arrSubKpiDec.push(subkpi.dec)

    }
    let sumJan = arrSubKpiJan.reduce((a, b) => a   b, 0);
    let sumFeb = arrSubKpiFeb.reduce((a, b) => a   b, 0);
    let sumMar = arrSubKpiMar.reduce((a, b) => a   b, 0);
    let sumApr = arrSubKpiApr.reduce((a, b) => a   b, 0);
    let sumMay = arrSubKpiMay.reduce((a, b) => a   b, 0);
    let sumJun = arrSubKpiJun.reduce((a, b) => a   b, 0);
    let sumJul = arrSubKpiJul.reduce((a, b) => a   b, 0);
    let sumAug = arrSubKpiAug.reduce((a, b) => a   b, 0);
    let sumSep = arrSubKpiSep.reduce((a, b) => a   b, 0);
    let sumOct = arrSubKpiOct.reduce((a, b) => a   b, 0);
    let sumNov = arrSubKpiNov.reduce((a, b) => a   b, 0);
    let sumDec = arrSubKpiDec.reduce((a, b) => a   b, 0);
    let arrMonths = [sumJan,
      sumFeb,
      sumMar,
      sumApr,
      sumMay,
      sumJun,
      sumJul,
      sumAug,
      sumSep,
      sumOct,
      sumNov,
      sumDec]
      return arrMonths
  }

so I m creating an array for each month of the year, then I pushing the values inside the single array always for each month, then with reduce() I'm doing the sum and in the end I'm returning the array. I know is really ugly to see it, but I don't know what to do to have the same behaviour. Thanks in advance

CodePudding user response:

You can try this approach with Object.entries and using an object instead of an array to keep your result values.

function totalMonth() {
  const result = {}
  const predefinedKeys = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'july', 'aug', 'sep', 'oct', 'nov', 'dec'];

  for (let subkpi of this.overview) {
    for (let [key, value] of Object.entries(subkpi)) {
      //if the key is not in predefined keys, we skip it
      if (!predefinedKeys.includes(key)) {
        continue;
      }

      //the object will be like result.jan, result.feb, and so on
      if (!result[key]) {
        result[key] = 0;
      }
      result[key]  = value; //sum directly here
    }
  }
  return predefinedKeys.map(key => result[key]);
}

  • Related