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

88
Views
Returning multiple objects without key

I am trying to return multiple objects without their index, however, I cannot find a way using Objects. This is what I have tried so far, am I missing something?

  const fields = [
    'name',
    'age',
    'address'
  ];

  const builtFields = []

  for (let i = 0; i < fields.length; i++) {
    builtFields.push(
      {
        [fields[i]]: { type: '' }
      }
    )
  }

  fields: Object.assign({}, builtFields)

  
  console.log(fields)

  Outputs:

  fields: {
     0: { name: { type: '' } },
     1: { age: { type: '' } },
     2: { address: { type: '' } }
  }

Desired output:

  fields: {
     name: { type: '' },
     age: { type: '' },
     address: { type: '' }
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're currently assigning the array entries to the object while you actually want to add each array item (which is an entry) to the empty object:

fields: Object.assign({}, ...builtFields)

As a shortest solution:

const fields = [
  'name',
  'age',
  'address'
];

const obj = Object.fromEntries(fields.map(x => [x, { type: '' }]));

console.log(obj);

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!