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

94
Vistas
Is there a concise way to write a set of enums as strings?

When i write enums in javascript I often write:

const movementState = {
  stopped: 'stopped',
  walking: 'walking',
  running: 'running',
};

This enables me to use it like movementState.stopped and linting ensures it exists, also if I log it, it's value is human readable.

I'd like something like:

enum movementState = {stopped, walking, running};

Note: I have no interest in doing this in typescript. I do my survival work-job in TS, I do my passion in JS.

I'm not super up to date w the standard... so I was hoping maybe something new was added in one of the stages or something. Thanks~

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

0

One option would be to map an array of the strings to an array of entries, using the string as the value and as the key, then turn it into an object with Object.fromEntries. Not extraordinarily concise, but I'm not sure if you can get anything better.

const makeEnum = strs => Object.fromEntries(
  strs.map(str => [str, str])
);

const movementState = makeEnum(['stopped', 'walking', 'running']);
console.log(movementState.running);

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