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

129
Vistas
Convert an object containing more objects into an array

This is what I am working with:

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}

I have been trying to compute my output result to look like this

result = [ 
    {"a": {"1": "2", "3": "4"}},
    {"b": {"3": "4", "5": "6"}},
    {"c": {"7": "8", "9": "10"}}
    {"d": {"11": "12", "13": "14"}}
]

I have tried the following which is closer but not the same as what I am looking for: Object.entries(object).map(([key, value]) => { return value })

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

0

Can be done using 2 forEach

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}



let res = []
Object.values(object).forEach((value) => {
    Object.entries(value).forEach((el) => {
    res.push({[el[0]]: el[1]})
  })
})


console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

Object.values(object)
    .flatMap(a => Object.entries(a))
    .map(([k, v]) => ({ [k]: v }))

Edited to use the last map idea from @Thomas instead of using Object.fromEntries

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this

let object = {'A':{"a": {"1": "2", "3": "4"},"b": {"3": "4", "5": "6"}},'B':{"c": {"7": "8", "9": "10"},"d": {"11": "12", "13": "14"}}};
let arr = Object.entries(Object.assign({}, ...Object.values(object))).map(([k, v]) => ({[k]: v}));
console.log(arr)

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