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

84
Vistas
spread two different objects with ternary

I have the following code and I want to manipulate sate under certain condition how to add the targeted state to the state object ? I tried tow options none has worked

const trueArr = { a: true, b: true, c:true };
const falseArr ={ a: false, b: false, c: false };
    
    setSate({
      ...iniciSate,
     [foo == 'test'  ? trueArr : falseArr]
    });
    
    
    setSate({
      ...iniciSate,
     {foo == 'test' ? ...trueArr : ...falseArr}
    });

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

This is a javascript question and not React.

To conditionally add this or that object's properties to another (using spread):

const state = {x:1}
const trueArr = { a: true, b: true, c:true }
const falseArr ={ a: false, b: false, c: false }

console.log(
    {...state, ...(true ? trueArr : falseArr)},

    // or "false"

    {...state, ...(false ? trueArr : falseArr)}
)

When you write ...(true ? trueArr : falseArr), then the surrounding parentheses evaluates it first, so the spread operator (...) "knows" which of the two options it applies to (the evaluated result)

about 4 years ago · Santiago Gelvez Denunciar

0

You were very close:

setSate({
  ...iniciSate,
 (foo == 'test') ? ...trueArr : ...falseArr
});
about 4 years ago · Santiago Gelvez 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