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

160
Vistas
How to assign value to var conditionally

I am looking for a way to do a conditional assignment of value to a key in an object. In my case i have an old data structure in my json file which is different then the new one so for example now the field is seller1FirstName and before it was seller1FName. What i need is a way to assign the value of this field to a var in my code. I know i could check if one exist and then assign it or if not use the other like this

if (data.seller1FirstName) {
   let mySellerFirstName = data.seller1FirstName
 } else {
   let mySellerFirstName = data.seller1FName
 }

i am hoping there is a cleaner and shorter way to do this

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

0

You could use a ternary 'if' operator to do the same thing in only one line:

let mySellerFirstName = data.seller1FirstName ? data.seller1FirstName : data.seller1FName;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the optional chaining operator in addition to the nullish coalescing operator.

let data = {
  seller1FName : 'value'
}

let newValue = data?.seller1FirstName ?? data.seller1FName;

console.log(newValue);

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