Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda