Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda