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

241
Views
Optimize solution for faster execution - NodeJs

I am trying optimize a solution which is used to group the objects based on value present in another array (mapping ) which is written is NodeJS.

function workerFunction(report) {
    const groupedArray = []
    const granularityKey = 'Month'
    const mapping = ['TestField1', 'TestField2'] //will be dynamic based on json input
    let activeIIndex = 0;
    while (activeIIndex < report.length) {
        const index = groupedArray
            .findIndex(item => mapping
                .every((column) => item[column] === report[activeIIndex][column]))
        if (index === -1) {
            report[activeIIndex][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost']
            groupedArray.push(report[activeIIndex])
        } else {
            groupedArray[index][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost']
        }
        activeIIndex++;
    }
    return groupedArray
}

The input to the workerFunction(report) will look like this (usually the array size will be above 300k),

[
   {
      "TestField1":"value",
      "TestField2":"value2",
      "Cost":12.5555,
      "Month":10
   },
   {
      "TestField1":"value3",
      "TestField2":"value4",
      "Cost":142.5555,
      "Month":10
   },
  {
      "TestField1":"value6",
      "TestField2":"value4",
      "Cost":15.87,
      "Month":10
   },
   {
      "TestField1":"value3",
      "TestField2":"value4",
      "Cost":16.5555,
      "Month":11
   }
]

The expected output after passing this json object through workerFunction will be

[
   {
      "TestField1":"value",
      "TestField2":"value2",
      "Cost":12.5555,
      "Month":10,
      "10":12.5555
   },
   {
      "TestField1":"value3",
      "TestField2":"value4",
      "Cost":142.5555,
      "Month":10,
      "10":142.5555,
      "11":16.5555
   },
  {
      "TestField1":"value6",
      "TestField2":"value4",
      "Cost":15.87,
      "Month":10,
      "10":15.87
   },
]
about 4 years ago · Juan Pablo Isaza
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!