Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!