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

148
Visualizações
Advanced Switch statement

I want to loop through an array of projects, filter out elements with multiple conditions, and push them to the destinated array. The result should have 4 arrays containing all the projects that met each specific array's condition. Something like this

// 4 cases of the parent array
const arrayOverDeadline_overBudget = []
const arrayOverDeadline_withinBudget = []

const arrayOneMonthDeadline_OverBudget = []
const arrayOneMonthDeadline_withinBudget = []

export const filterArray = (array) => {
  const today = moment();
  const oneMonthBefore = moment().add(1, "m");

  array.forEach((item) => {
    switch (item.deadline && item.budget) {
      // over deadline && over budget
      // (deadline <= today) && (budget > 100%)
      case params.deadline <= today && params.budget >= 100:
        break;

      // over deadline && within budget 
      // (deadline <= today) && (80% < budget < 100%)
      case params.deadline < today &&
        params.budget < 100 &&
        params.budget > 80:
        break;

      // within deadline && within budget 
      // (today < deadline < oneMonthBefore) && (80% <= budget < 100%)
      case params.deadline > today &&
      params.deadline < oneMonthBefore &&
      params.budget < 100 &&
      params.budget >= 80:        
      break;

      // within deadline && over budget
      // (today < deadline < oneMonthBefore) && (budget >= 100%)
      case params.deadline > today &&
      params.deadline < oneMonthBefore &&
      params.budget > 100:
        break;

      default:
        break;
    }
  });
};

I looked for some answers and found Flags and Bitmasks but wasn't sure how to implement it in my cases. I also worry about the time complexity of my method. Any tips or ideas would be very welcome and appreciated.

Update:

  1. Added conditions.
  2. The purpose of the question was to ask whether there are more efficient ways to distribute the elements in the array to the desired location. I have tried the if..elseif...else approach but I just wanted to know if there are "cleaner" ways, and it doesn't have to use switch statement.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply break this down to 2 boolean variables:

  1. bool1 = is project over the deadline
  2. bool2 = is project over the budget

Create an int from these variables (bool1 right shifted once + bool2) to get a value between [0,3], and then you can map this value to the target array.

Example:

  1. bool1 = 0, bool2 = 0. Target value = 00 (binary) = 0. Target value 0 maps to under deadline, under budget.
  2. bool1 = 0, bool2 = 1. Target value = 01 (binary) = 1. Target value 1 maps to under deadline, over budget.
  3. bool1 = 1, bool2 = 0. Target value = 10 (binary) = 2. Target value 2 maps to over deadline, under budget.
  4. bool1 = 1, bool2 = 1. Target value = 11 (binary) = 3. Target value 3 maps to over deadline, over budget.

You can generate this target value using just one line of code, and then using these cases to decide the destination array should remove the need for writing complicated if-else blocks.

Edit:

Adding a sample implementation:

const budget = 100,
  deadline = 100
const item_budget = 120,
  item_deadline = 80
const b1 = Number(item_deadline > deadline)
const b2 = Number(item_budget > budget)
const target = (b1 << 1) + b2
switch (target) {
  case 0:
    console.log("under deadline, under budget")
    break;
  case 1:
    console.log("under deadline, over budget")
    break;
  case 2:
    console.log("over deadline, under budget")
    break;
  default:
    console.log("over deadline, over budget")
}
// prints "under deadline, over budget"

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