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

258
Visualizações
How to create JSON from inputs name and value pairs

I'm working on my personal project and I need a little help. I have a table with some inputs which have name and value attributes. The problem is that I need to create a JSON with pairs name:value. How do I do something like that with vanilla JS?

My code to print what I have:

// Get all inputs from table and print name and value
document.querySelectorAll("table input").forEach((e) => {
    console.log(e.name, e.value);
});

What I need:

{
  name1: value1,
  name2: value2,
  name3: value3,
  name4: value4,
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Start out with an empty object and then, when you're iterating over the inputs, just assign a new key value pair for that object.

const obj = {}

// Get all inputs from table and print name and value
document.querySelectorAll("input").forEach((e) => {
  obj[e.name] = e.value
})

console.log(obj)
<input name="foo" value="foo">
<input name="bar" value="bar">

Use JSON.stringify(obj) to convert it in to a JSON string if needed.

about 4 years ago · Santiago Trujillo 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