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

326
Vistas
convert slightly complex "if" statement to ternary operator

I have the following code from a project:

setTweetLibrary((curr) => {
    if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
        console.log("update");
        return saved_tweets;
    } else {
        console.log("not update");
        return curr;
    }
});

I would like to convert this to a ternary operator with a question mark ("?") to make it neater, but I can't figure out how. Can anyone advise? Thank you!

I think I'm just really confused on how the syntax of ternary operators with multiple conditions and values

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As ternary it would look like this:

setTweetLibrary((curr) => {
      return !curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)
        ? saved_tweets
        : curr;
});

Note that there is no more place for console.logs. But they seem to be debug anyway.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The ternary operator is not a generic replacement for an if statement, only for choosing between expressions that are going to the same destination.

If we take out the logging, both branches are returning a variable:

if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
    return saved_tweets;
} else {
    return curr;
}

The fact that the condition is a long expression doesn't make any difference, it's still equivalent to this:

if (some_condition_goes_here) {
    return saved_tweets;
} else {
    return curr;
}

In that case, you could use a ternary operator to select between the two - move the return to where the if is, and the ? and : before the two values. Then put the whole thing in parentheses to make sure the whole thing is calculated before return:

return (
    (some_condition_goes_here)
    ? saved_tweets
    : curr
);

Specifically:

return (
    (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id))
    ? saved_tweets
    : curr
);

It might be possible to add the logging in there as well, but it will be much harder to read than just using a normal if statement, so if you want that - or any other logic - just stick with if...else.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This works

 return (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id) ? saved_tweets : curr)
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