Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda