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

164
Views
Converting an arrays to an object without keys

I am looking for a way to i convert this.

    const data = [{
                id: 1,
                fullnames: 'JosephSam',
                weight: 1231,
                currentdate: '2021-09-22'
    }]

into this

        const data = {
                id: 1,
                fullnames: 'JosephSam',
                weight: 1231,
                currentdate: '2021-09-22'
    }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const arr = [{
            id: 1,
            fullnames: 'JosephSam',
            weight: 1231,
            currentdate: '2021-09-22'
}];

const data = arr[0];

console.log(data.fullnames); // 'JosephSam'

This would be what you need. I do get the feeling this is isn't what you're looking for. If so, please give us a more complex example to see what it is you're dealing with.

about 4 years ago · Juan Pablo Isaza Report

0

If you have multiple values inside your array, then you can have a separate object to insert each array value into it by having id as the object key.

Take a look at the following example.

const data = [
  {
    id: 1,
    fullnames: "JosephSam",
    weight: 1231,
    currentdate: "2021-09-22",
  },
  {
    id: 2,
    fullnames: "Jane",
    weight: 1431,
    currentdate: "2021-09-22",
  },
];

const mappedData = {};

data.forEach((element) => (mappedData[element.id] = element));

console.log("Mapped Data: ", mappedData);

Console log

Mapped Data:  {
  '1': {
    id: 1,
    fullnames: 'JosephSam',
    weight: 1231,
    currentdate: '2021-09-22'
  },
  '2': { id: 2, fullnames: 'Jane', weight: 1431, currentdate: '2021-09-22' }
}

Then you can refer each element by id as follows. (x is the id you need to get)

mappedData[x]
about 4 years ago · Juan Pablo Isaza Report

0

I'm not sure what are you trying to do. But If you have a single indexed array you should to this:

var data = [{
    id: 1,
    fullnames: 'JosephSam',
    weight: 1231,
    currentdate: '2021-09-22'
}]
data = data[0];
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!