Search code examples
javascriptarraysdata-structuresmappingdry

How would one apply the concept of DRY for repetitive array mapping tasks?


Given following code ...

// last 7 days data from db ...

let last7Days =
  result.map((data) => data._id).slice(1).slice(-7);

let last7daysIncome =
  result.map((data) => data.totalIncomeAmount).slice(1).slice(-7);

let last7daysAvgIncome =
  result.map((data)=> data.avrageIncome).slice(1).slice(-7);

let last7daysPatientionsCount =
  result.map((data)=> data.PatientionsCount).slice(1).slice(-7);

let last7disease =
  result.map((data)=>data.diseaseArr).slice(1).slice(-7);

... how would one simplify this code with the DRY Concept (Don't Repeat Yourself) in JavaScript?


Solution

  • Since the OP obviously maps 5 times an array of data items, each time collecting another specific property value of such an item, one could come up with an approach which solves merging and collecting item entries in a generic way (agnostic to the underlaying data structure).

    Merging array items into an object of grouped arrays is a classic reduce task which, with the next provided solution (due to its generic implementation) is done twice.

    The following implementation in addition uses Object.entries in order to access each entry's key-value pair directly. It also utilizes the Logical nullish assignment / ??= operator in order to access or create/assign an array with one line of code.

    function mergeAndCollectItemEntries(result, item) {
      return Object
        .entries(item)
        .reduce((merger, [key, value]) => {
    
          (merger[key] ??= []).push(value);
          return merger;
    
        }, result);
    }
    
    const sampleData = [
      { _id: 'foo', totalIncomeAmount: 10_000, last7daysAvgIncome: 2_500, PatientionsCount: 123, diseaseArr: ['long', 'sick', 'leave'] },
      { _id: 'bar', totalIncomeAmount: 8_000, last7daysAvgIncome: 2_000, PatientionsCount: 99, diseaseArr: ['very', 'long', 'sick', 'leave'] },
      { _id: 'baz', totalIncomeAmount: 4_000, last7daysAvgIncome: 1_000, PatientionsCount: 50, diseaseArr: ['really', 'long', 'sick', 'leave'] },
    ];
    
    const {
    
      _id: last7Days,
      totalIncomeAmount: last7daysIncome,
      last7daysAvgIncome: avrageIncome,
      PatientionsCount: last7daysPatientionsCount,
      diseaseArr: last7disease,
    
    } = sampleData.slice(-7).reduce(mergeAndCollectItemEntries, {});
    
    console.log({
      last7Days,
      last7daysIncome,
      avrageIncome,
      last7daysPatientionsCount,
      last7disease,
    } );
    
    // was before ...
    //
    // // last 7 days data from db...
    // 
    // let last7Days =
    //   result.map((data)=>data._id).slice(1).slice(-7);
    // 
    // let last7daysIncome =
    //   result.map((data) => data.totalIncomeAmount).slice(1).slice(-7);
    // 
    // let last7daysAvgIncome =
    //   result.map((data)=> data.avrageIncome).slice(1).slice(-7);
    // 
    // let last7daysPatientionsCount =
    //   result.map((data)=> data.PatientionsCount).slice(1).slice(-7);
    // 
    // let last7disease =
    //   result.map((data)=>data.diseaseArr).slice(1).slice(-7);
    .as-console-wrapper { min-height: 100%!important; top: 0; }