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

295
Visualizações
reusing data in function so it is not as repetitive in javascript

I have this function, I am excluding some of the logic since it is not all relevant to my initial question. The main question is how I can optimise code to not be as repetitive. I was thinking to set them as variables to reuse but since the data is different it will not provide correct results. Any suggestions or examples that can point me in the right direction would be appreciated.

function myFunction() {
const checkOne = condition && condition
const checkTwo = condition && condition
  if (checkOne || checkTwo) {
    let someuniquestringone;
    let someuniquestringtwo;
    if (checkOne) {
      ...
      const randomData = function({
        moreData: 'STRING',
        moreUniqueDataOne: moreUniqueDataOne,
      });
      someMethod(
        insideFunction({
          evenMoreData: evenMoreData,
          evenMoreUniqueDataOne: 'someuniquestringone',
        }),
      );
    }
    if (checkTwo) {
      ...
      const randomData = function({
        moreData: 'STRING',
        moreUniqueData: moreUniqueDataTwo,
      });
      someMethod(
        insideFunction({
          evenMoreData: evenMoreData,
          evenMoreUniqueDataTwo: 'someuniquestringtwo',
        }),
      );
    }
    ....
  }
  ....
}

My main observation was that I can reuse this so it is not as repetitive:

      const randomData = function({
        moreData: 'STRING',
        moreUniqueDataOne: moreUniqueDataOne,
      });
      someMethod(
        insideFunction({
          evenMoreData: evenMoreData,
          evenMoreUniqueDataOne: 'someuniquestringone',
        }),
      );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should group your conditions and data somehow to be able to associete them without hardcoding variables for each individually (moreUniqueData, someuniquestring). I'd suggest using a json array for that. Then you can just iterate over your array or filter out any condidtion that is not true. Something like that maybe:

function myFunction() {
  //this the association array which connects conditions with their data and strings
  const checks = [
    {condition: condition && condition, string: someuniquestringone, moreUniqueData: {/* some data */}, evenMoreData: {/* some data */}},
    {condition: condition && condition, string: someuniquestringtwo, moreUniqueData: {/* some data */}, evenMoreData: {/* some data */}}
    /* maybe even more conditions and data */
  }
  /* find only the checks that are true, because others are not used in your code. 
    If none is true, the array of trueChecks will be empty and nothing is executed
  */
  const trueChecks = checks.filter(check => {return check.condition === true});
  //iterate over the true entries with their associeted data:
  for (let check of trueChecks ) {
    const randomData = {moreData: 'STRING', ...check.moreUniqueData};
    someMethod(
      insideFunction({
        evenMoreData: check.evenMoreData,
        evenMoreUniqueDataOne: check.string,
      }),
    );
  }
  ....
}
about 4 years ago · Juan Pablo Isaza Relatório
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