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

257
Vistas
convert object keys and values in single Array JS?

I have an object that looks like this:

const dataObj =
{"Tom-29":[ 
    { "number": 12, "string": "hi"},
    { "number": 40, "string": "bye"}
]}

I want something like below and result sorted by number

[
   Name:"Tom",
   Age:29,
    [{"number": 12, "string": "hi"},
    {"number": 40, "string": "bye"}]
];

How would i do this ? I tried below approach .

 const arr =Object.entries(dataObj).map(itm=>({...itm,Name:itm[0]}));

Please help me to resolve

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Iterate the dataObj entries with Array.flatMap(), extract the name and age from the key, and then map the values, and add the properties to the original object using array spread:

const dataObj = {"Tom-29":[{"number":12,"string":"hi"},{"number":40,"string":"bye"}]}

const result = Object.entries(dataObj)
  .flatMap(([key, values]) => {
    const [name, age] = key.split('-')
    
    return values.map(v => ({
      name,
      age: +age,
      ...v
    }))
  })
  
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the object key and append it to each of it's children as a name.

    const dataObj =
    {"Tom-29":[ 
        { "number": 12, "string": "hi"},
        { "number": 40, "string": "bye"}
    ]}

    const results = Object
      .keys(dataObj)
      .map(k => { 
        dataObj[k].forEach(i => { 
          i.name = k.split('-')[0]; 
          i.age = k.split('-')[1]; 
        }); 
        return dataObj[k] 
      })
      .flat();

    console.log(results);

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