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

144
Views
loop throught an object and return a array of object

How can I loop over this object(data) and return a an array of object (output) just like this. basically with these specific properties and value

data = {
"model-10389": 164703,
"model-10388": 164704,
"model-10387": 164705,
}


const output  = [
{
  modelId : 10389,
 id : 164703
},
{
  modelId : 10388,
  id : 164704
},
{
  modelId : 10387,
  id : 164705
},
]

this is what I have now

Object.keys(data).map(function(key, index) {
 console.log(data[key])
});

or this

for (const property in data) {
console.log(`${property}: ${data[property]}`);
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Split the problem up into small parts:

  1. Get all "entries" of the source object.
  2. These entries need to be transformed, we can use Array.map for that
  3. Every entry is an [key, value] array, we can use destructuring in the .map for easy access.
  4. For the modelId, we need to .split the key, and get the part behind the -, then parse it as a number.
  5. The id is just the value.

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const result = Object.entries(data)
  .map(([key, value]) => ({
    modelId: parseInt(key.split('-')[1], 10),
    id: value
  }));

console.log(result);

A slightly faster alternative to the split, id to use a regex to extract ids:

modelId: parseInt(key.match(/\d+/)[0], 10),

However, if we're going to go as far as we can to optimize this transform, we're going to have to make a few more changes:

  • Unary + instead of parseInt.
  • for in loop instead of Object.entries and .map.
  • Regex instead of .split

That'll get you:

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const result = [];
for (let key in data) {
  result.push({
    modelId: +key.match(/\d+/)[0],
    id: data[key]
  });
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I'd use Object.entries with object destructuring, short object literal syntax and unary + for conversion to number:

const data = {"model-10389": 164703,  "model-10388": 164704,  "model-10387": 164705};

const output = Object.entries(data).map(([key, id]) => ({
    modelId: +key.replace(/.*-/, ""),
    id
}));

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

If the key(model-*) in data doesn't change, then you can use substring() with the index.

const data = {
  "model-10389": 164703,
  "model-10388": 164704,
  "model-10387": 164705
}

const result = Object.entries(data)
                .map(([key, value]) => ({
                  modelId: +key.substring(6),
                  id: value
                }));
                
console.log(result)

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!