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

104
Vistas
Get object property name with corresponding value

I have a field object that can contain only one of different properties with an id as a value.

const Field = {
  // Field can contain
  property1Id: 'someId',
  // Or 
  property2Id: 'someOtherId',
  // Or
  property3Id: '...'
  //...
};

I want to return the property name and it's value. The following works fine but feels a bit long. Anyway to reduce it / be more efficient.

const propertyName = Field.property1Id
  ? 'Property 1'
  : someObject.property2Id
  ? 'Property 2'
  : someObject.property3Id
  ? 'Property 3'
  : 'Other';

const id = Field.object1Id
  ? Field.property1Id
  : Field.property2Id
  ? Field.property2Id
  : Field.property3Id
  ? Field.property3Id
  : null;

console.log(propertyName, id)

Thanks.

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

0

If this is what you mean, every property name with property value is returned :)

const Field = {
   // Field can contain
  property1Id: 'someId',
  // Or 
  property2Id: 'someOtherId',
  // Or
  property3Id: '...'
  //...
}

let entries = Object.entries(Field);
entries.forEach(entry => {
  console.log(entry)
})

about 4 years ago · Juan Pablo Isaza Denunciar

0

Best approach

const Field = {
  // Field can contain
  property1Id: 'someId',
  // Or 
  property2Id: 'someOtherId',
  // Or
  property3Id: '...'
  //...
};

let i = 0;
for (const property in Field) {
  i++
  console.log('Property ' + i, Field[property]);
}

Output: 
"Property 1" "someId"
"Property 2" "someOtherId"
"Property 3" "..."
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