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

102
Vistas
How to change object of object of array objects to array objects using javascript

I would like to know how to flatten the array of objects without flap map in javascript

If the of object of arrays object has length greater than 1, flatten

in my example property black has more than object so return that in array of objects

var obj ={
    "details": {
        "black": [
            {
            value: 100,
            name: "xxx"
            },
            {
            value: 200,
            name: "yyy"
            }
        ]
    },
    "sales": {
        "blue": [
            {
               value: 50,
               name: "abc"
            }
        ],
        "ALL": [
            {
              value: 20,
              name: "100"
            }
        ]
    }
}

Expected Output

[
  {
    value: 100,
    name: "xxx"
  },
  {
    value: 200,
    name: "yyy"
  }
]

have tried

const result = Object
    .values(obj)
    .flatMap(v => 
        Object.values(v as any)
            .filter((group: any) => group.length > 1)
    )

without flapmap how to do using javascript

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

0

You can create a recursive flatten function using Array.reduce() that takes a multidimensional array, and converts it to a flat array (TS playground):

const obj = {"details":{"black":[{"value":100,"name":"xxx"},{"value":200,"name":"yyy"}]},"sales":{"blue":[{"value":50,"name":"abc"}],"ALL":[{"value":20,"name":"100"}]}}

const flatten = arr => 
  arr.reduce((acc, a) => acc.concat(
    Array.isArray(a) ? flatten(a) : a
  ), [])

const result = flatten(Object.values(obj)
  .map(v => 
    Object.values(v)
      .filter(group => group.length > 1)
  )
)

console.log(result)

Note: you can also use Array.flat() but since it came out with Array.flatMap() you would probably still have a compatibility problem.

about 4 years ago · Juan Pablo Isaza Denunciar

0

var obj ={
    "details": {
        "black": [
            {
            value: 100,
            name: "xxx"
            },
            {
            value: 200,
            name: "yyy"
            }
        ]
    },
    "sales": {
        "blue": [
            {
               value: 50,
               name: "abc"
            }
        ],
        "ALL": [
            {
              value: 20,
              name: "100"
            }
        ]
    }
}

console.log(obj.details.black)

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