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

105
Visualizações
How can I create conditions on multiple array objects to handle errors in React?

I have an array object called containtemps. It has a property value called modDate.

I extract the boolean value of the temperror variable by comparing the time based on the modDate property value among several objects in containtemps.

So, by putting temperror variables 1, 2, and 3 in the error variable, if any one of them is true, the NotError component is rendered, and when all of them are false, the Error component is rendered.

But looking at my code, this seems inefficient because there are too many duplicates.

As you can see, temperror0 and temperror1 and temperror2, The code that creates the variable is duplicated. How can I get rid of these duplicate values ​​in one piece of code?

this is my code

    const containtemps = [
        {
            modDate:"2022-09-22T04:48:00.000Z"
        },
        {
            modDate: null
        },
        {
            modDate:"2022-09-22T04:5:00.000Z"
        }
    ]

      this is duplicated code

      let curTime = new Date()
      curTime.setHours(curTime.getHours() - 6)


    const tempModate0 = containtemps[0]?.modDate;

    let tempmodDate0 = new Date(tempModate0)

    let temperror0 = curTime.getTime() <= tempmodDate0.getTime() ? false : true


    const tempModate1 = containtemps[1]?.modDate;


    let tempmodDate1 = new Date(tempModate1)

    let temperror1 = curTime.getTime() <= tempmodDate1.getTime() ? false : true



    
    const tempModate2 = containtemps[2]?.modDate;


    let tempmodDate2 = new Date(tempModate2)

    let temperror2 = curTime.getTime() <= tempmodDate2.getTime() ? false : true

    const error =

    temperror0 ||
    temperror1 ||
    temperror2 ;

    ;


    return (

    !error ?
    (
    <NotError>
    notError
    </NotError>

    )
    :
    (
        <Error>
        error
        </Error>)

    )
almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use some function inside your array. It will return true if it finds at least one result matching your expression. Here you need the opposite expression >

const curTime = new Date();
const error = containtemps.some(t => t.modDate && curTime.getTime() > new Date(t.modDate).getTime())
almost 4 years ago · Santiago Trujillo Relatório

0

You could just declare an helper function:

const checkTempDate = (tempDate) => {
  const date = new Date(tempDate);
  return curTime.getTime() <= date.getTime() ? false : true;
};

const error =
  checkTempDate(containtemps[0]?.modDate) ||
  checkTempDate(containtemps[1]?.modDate) ||
  checkTempDate(containtemps[2]?.modDate);

return !error ? <NotError>notError</NotError> : <Error>error</Error>;
almost 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