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

185
Visualizações
how to organize big conditional array

i have to create array with a lot of conditions. but when i try to read that what i wrote it's very complicated to know what i'm try to doing. so i try to simplify to this conditions but nothing comes to my mind. placements are important too. how can i simplify this code block?

const createArrayByConditions =
 (condition1, condition2, condition3, condition4) => {

  if (condition1) {
   if (condition4) {
    return [
     1, 4, 999,
    ];
   } else {
    return [1, 999];
   }
  }

  if (condition2) {
   if (condition4) {
    return [
     2, 4, 999,
    ];
   }
   return [2, 999];
  }

  if (condition3) {
   if (condition4) {
    return [
     3, 4, 999,
    ];
   } else {
    return [3, 999];
   }
  }

  if (condition4) {
   return [4, 999];
  } else {
   return [999];
  }
 };
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

A fairly clean solution comes to mind. It seems that the return array always contains 999, so we start with an array with just that value. 4 is also always included as long as condition4 is truthy. Lastly we need to prefix 1, 2, or 3, based on which conditionN evaluates as a truthy value first.

function createArrayByConditions(
  condition1, condition2, condition3, condition4
) {
  const array = [999];
  
  if (condition4) array.unshift(4);
  
  if      (condition1) array.unshift(1);
  else if (condition2) array.unshift(2);
  else if (condition3) array.unshift(3);
  
  return array;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Overall it seems like there is nothing to simplify. You have your conditions and the complexity is there. If you can't make more assumptions on the returned values (they are examples, I assume), then you are good with what you have.

I personally prefer ternary operators if every conditional path eventually returns a value of the same type. But I know you can easily shoot yourself in the foot with them. However, you may decide on the readability:

const createArrayByConditions = (c1, c2, c3, c4) => c1
  ? (c4 ? [1, 4, 999] : [1, 999])
  : c2
  ? (c4 ? [2, 4, 999] : [2, 999])
  : c3
  ? (c4 ? [3, 4, 999] : [3, 999])
  : (c4 ? [4, 999] : [999]);
about 4 years ago · Juan Pablo Isaza Relatório

0

This is the furthest I could simplify the conditions by looping the arguments passed as an array:

const createArrayByConditions =
 (conditions) => {
  for(let i=0;i<3;i++){
    if ( conditions[i] && conditions[3] ){
      return [i+1, 4, 999]
    }
    else if( conditions[i] ){
      return [i+1, 999];
    }
  }  
  if(conditions[3])
    return [4, 999];
  else
    return [999];
 };

 //1-4
 console.log( createArrayByConditions([true, false, false, false]) );   //-> [1,999]
 console.log( createArrayByConditions([true, false, false, true]) );    //-> [1,4,999]

 //2-4
 console.log( createArrayByConditions([false, true, false, false]) );   //-> [2,999]
 console.log( createArrayByConditions([false, true, false, true]) );    //-> [2,4,999]

 //3-4
 console.log( createArrayByConditions([false, false, true, false]) );   //-> [3,999]
 console.log( createArrayByConditions([false, false, true, true]) );    //-> [3,4,999]
 
 //4
 console.log( createArrayByConditions([false, false, false, true]) );   //-> [4,999]
 console.log( createArrayByConditions([false, false, false, false]) );  //-> [999]

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