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

247
Visualizações
Node.js/Express how to modify data obtained from checkbox form input

I have a list of input checkbox elements rendered with handlebars

<form method="POST" action='/processImages'>
                        <input type='checkbox' id='.DS_Store' name='.DS_Store' />
                            <p>.DS_Store</p>
                        <input type='checkbox' id='.localized' name='.localized' />
                            <p>.localized</p>
                        <input type='checkbox' id='Courses' name='Courses' />
                            <p>Courses</p>
                        <input type='checkbox' id='Home' name='Home' />
                            <p>Home</p>
                    
<input type="submit" value="run">
         

and i want to pass the names to the other function or render their names on other path With this code:

const processRouter = express.Router()

const processRouter = express.Router()
processRouter.post('/', async (req, res, next) => {
    const test =(req.body) 
    res.send(test)
})

app.use('/processImages', processRouter)

what im getting is:

{"Courses":"on","Home":"on"}

But i want to get just the names like {"Courses", "Home"} How to get rid of this "on" on each element, i'm assuming this is comming from input type checkbox.

I'm a beginner and i cant really understand how i should request only "names". Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It would not be possible to get {"Courses", "Home"} because this is invalid JSON format. You could however pass an array ["Courses", "Home"] through the request instead.

If the request that your form is sending is not something you want - consider writing your own onSubmit function to handle the AJAX call.


Nonetheless, is there a particular reason why you want to modify the request? You could parse the original format to result in what you want:

function iterate(input) {
    //Example input: {"Courses":"on","Home":"on"}

    const arr = [];

    Object.entries(input).forEach(function ([key, value], index) {
        console.log(key, value, index);
        if (value === "on")
            arr.push(key);
    });

    return arr;
}

console.log(iterate({ "Courses": "on", "Home": "on" }));
console.log(iterate({ "Courses": "on", "Home": "off" }));
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