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

92
Visualizações
If statement with multiple || conditions

I am looping through an array of objects and trying to see if there is a better way to write this (i feel like there is).

This is my function below.

  getCallInfo = (callSch: any, security: any) => {
    const callObj = {
      CallTimingType: 'Not Callable',
      isCheckMark: null,
    };

    if (!security.IsCallable) return callObj;

    callSch.Calls.forEach((el: { CallTimingType: string }) => {
      if (el.CallTimingType === CallTimingType.SpecificDates) {
        callObj.CallTimingType = 'Discrete';
        callObj.isCheckMark = true;
      }
      if (
        el.CallTimingType === CallTimingType.AnyTime ||
        el.CallTimingType === CallTimingType.Monthly ||
        el.CallTimingType === CallTimingType.OnPaymentDates ||
        el.CallTimingType === CallTimingType.AnyInterestAdjustmentDate
      ) {
        callObj.CallTimingType = 'Continuous';
        callObj.isCheckMark = true;
      }
    });

    return callObj;
  };

basically what is doing is returning the default object if security.IsCallable is false but if true looping through the array of objects and setting the default object to different values. What I am basically doing is trying to refactor this. CallTimingType is an object with these enum values. Any thoughts?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could use Array.prototype.some() to iterate and test if el.CallTimingType matches any value.

if (
  el.CallTimingType === CallTimingType.AnyTime ||
  el.CallTimingType === CallTimingType.Monthly ||
  el.CallTimingType === CallTimingType.OnPaymentDates ||
  el.CallTimingType === CallTimingType.AnyInterestAdjustmentDate
)

Can be rewritten as:

const callTimingTypes = [
     CallTimingType.AnyTime,
     CallTimingType.Monthly,
     CallTimingType.OnPaymentDates,
     CallTimingType.AnyInterestAdjustmentDate
];

if (callTimingTypes.some((t) => el.CallTimingType === t))
about 4 years ago · Santiago Trujillo Relatório

0

You could rewrite your clause with switch case operator to make it clear:

callSch.Calls.forEach((el: { CallTimingType: string }) => {
  switch (el.CallTimingType) {
    case CallTimingType.SpecificDates:
      callObj.CallTimingType = 'Discrete';
      callObj.isCheckMark = true;
      break;
    case CallTimingType.AnyTime:
    case CallTimingType.Monthly:
    case CallTimingType.OnPaymentDates:
    case CallTimingType.AnyInterestAdjustmentD:
      callObj.CallTimingType = 'Continuous';
      callObj.isCheckMark = true;
      break;
  }
});

Alternatively, you could use Set:

callSch.Calls.forEach((el: { CallTimingType: string }) => {
  const discreteSet = new Set([CallTimingType.SpecificDates]);
  const continuousSet = new Set([
    CallTimingType.AnyTime,
    CallTimingType.Monthly,
    CallTimingType.OnPaymentDates,
    CallTimingType.AnyInterestAdjustmentD,
  ]);

  if (discreteSet.has(el.CallTimingType)) {
    callObj.CallTimingType = 'Discrete';
    callObj.isCheckMark = true;
  } else if (continuousSet.has(el.CallTimingType)) {
    callObj.CallTimingType = 'Continuous';
    callObj.isCheckMark = true;
  }
});
about 4 years ago · Santiago Trujillo 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