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

286
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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