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');
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);