Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda