Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Obtener el nombre de la propiedad del objeto con el valor correspondiente

Tengo un objeto de campo que puede contener solo una de diferentes propiedades con una identificación como valor.

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

Quiero devolver el nombre de la propiedad y su valor. Lo siguiente funciona bien, pero se siente un poco largo. De todos modos para reducirlo / ser más eficiente.

 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)

Gracias.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Si esto es lo que quiere decir, se devuelve cada nombre de propiedad con valor de propiedad :)

 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 Report

0

Mejor enfoque

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!