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

115
Vistas
How to Convert Nested Object in Key Value Pair in Javascript

I want to do this with programming

Problem

  attributes: {
    allow: [ '0' ],
    create: [ '0' ],
    all: [ '0' ],
    rfid: [ '0' ],
}

What I want is

  attributes: {
    allow:'0',
    create: '0',
    all:'0',
    rfid: '0',
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Functional approach:

You can use Object.entries to turn an object { a: 1, b: 2 } into an array of arrays (containing pairs of keys and values) [['a', 1], ['b', 2]]. You can then use Array#map to apply an operation on each and mapping each element of the array to the return value of the operation. In our case, we'll actually have something like [['allow', ['0']], ...] at that point and want to map it to [['allow', '0'], ...]. Then at the end, we undo the first operation using Object.fromEntries to get an object back.

The mapping operation can be simply ([k, [v]]) => [k, v]. Using destructuring, we get an expressive way of extracting the value that we need and returning it.

Now since the whole thing seems to be inside another object with attributes as key (and I don't know if there can be other keys), we'll return another such object with attributes replaced and the other keys preserved:

function transform (object) {
  return {
    ...object,
    attributes: Object.fromEntries(
      Object.entries(object.attributes).map(([k, [v]]) => [k, v])
    )
  }
}

Imperative approach:

This approach is simpler, but will mutate your object. You can loop over the keys in the attributes object and replace each value with its own first array element, as follows:

function transform (object) {
  for (const key in object.attributes) {
    object.attributes[key] = object.attributes[key][0]
  }
  
  return object
}
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