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

119
Vistas
converting enum object to array returns extra object in javascript

I would like to know how to convert enum object to array of objects in javascript

It works, but am getting the output with four object arrays instead of two object arrays

const result = Object.entries(countries).map(([value, key]) =>
   ({ country: value.toLowerCase(), id: key })
);

enum countries {
  SINGAPORE = 123,
  DENMARK = 246
}

Actual Output:

[
  {country: '123', id: 'singapore'}
  {country: '246', id: 'denmark'}
  {country: 'singapore', id: 123}
  {country: 'denmark', id: 246}
]

Expected Output:

[
  {country: "singapore", id: 123},
  {country: "denmark", id: 246}
]
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's important to understand the JavaScript that typescript generates when you use enum - yours will look like this:

{
    "123":"SINGAPORE",
    "246":"DENMARK",
    "SINGAPORE":123,
     "DENMARK":246
}

Which should make it clear why you get 4 items when you enumerate it using Object.entries (or Object.keys etc).

You can filter out the numeric values if you wish

var countries = {
        "123":"SINGAPORE",
        "246":"DENMARK",
        "SINGAPORE":123,
         "DENMARK":246
    }

const result = Object.entries(countries)
                    .filter(([key,_]) => !isNaN(key))
                    .map(([key, value]) => ({ country: value.toLowerCase(), id: key }));
                    
console.log(result);

over 4 years ago · Santiago Trujillo 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