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

103
Views
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 answers
Answer question

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 Report

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 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!