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

282
Views
Better way to turn object with named booleans into array with their uppercase keys?

I'm trying to convert the methods part of an Express Router into a one-dimensional array of uppercase string words, so this:

layer.route.methods: { get: true, post: true }

into:

methods: ["GET", "POST"]

This is what I came up with, but isn't there a more subtle way?

const methods = [];
!layer.route.methods.get || methods.push('GET');
!layer.route.methods.post || methods.push('POST');
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This can be achieved with a combination of filter (to get only the methods with true) and map (to capitalize).

const methods = { get: true, post: true };

const result = Object.keys(methods).filter(method => methods[method]).map(method => method.toUpperCase());

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Turn down object to an array by using Object.keys(object) and then use loop either for or map to iterate and check if item exist in that converted array convert then to uppercase using item.toUpperCase()

about 4 years ago · Juan Pablo Isaza Report

0

One of the ways is to use reducer, to filter and modify values in one iteration.

const inputObject = { get: true, post: true };

const methods = Object.entries(inputObject).reduce(
  (accumulator, [key, value]) => {
    if (value) accumulator.push(key.toUpperCase());
    return accumulator;
  },
  []
);

console.log(methods);

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!