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

180
Views
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 answers
Answer question

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 Report

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 Report

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 Report
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!