Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Create a function, send in the parameter for the field you want to extract, and even the number of days to look back:

function lastDays(result, field, days) {
  return result.slice(days).map((data)=>data[field]);  
}
function lastSevenDays(result, field) {
  return lastDays(result, field, -7);
}

let last7Days = lastSevenDays(result, "_id")
let last7daysIncome = lastSevenDays(result, "totalIncomeAmount")
let last7daysAvgIncome  =lastSevenDays(result, "avrageIncome")
let last7daysPatientionsCount  = lastSevenDays(result, "PatientionsCount")
let last7disease = lastSevenDays(result, "diseaseArr")
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!