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

153
Views
Best approach in extracting the values of array inside object in js

What is best approach in extracting the below object. I tried two ways. Can this be refactored in any other way to optimize the performance?

const data = {
    "postcode": [
        "123456"
    ],
    "time": [
        "05:11"
    ]
};

Approach 1:

for (const element of Object.entries(data)) {
    const key = element[0];
    const value = element[1][0];
    data[key] = value;
}

Approach 2:

for (const key in data) {
    if (Object.hasOwnProperty.call(data, key)) {
        data[key] = data[key][0];
    }
}

console.log('data ', data);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A concise method is this:

for (const [key, [value]] of Object.entries(data)) {
    data[key] = value
}

Notice the extra set of [] around [value] to extract the value from that array immediately too.

Of course, conciseness isn't always better and you may prefer your other solutions if you think they are more readable.

In terms of performance this isn't any better than your first solution. Your second option will be more memory efficient though it heavily depends on the size and shape of your object.

about 4 years ago · Juan Pablo Isaza Report

0

This approach will solve your problem. MDN recommended!

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"
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!