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

317
Visualizações
renaming keys / values in objects dynamically

I'm trying to put the values from the 'allValues' array inside the existing keys in the memory object, but instead they are just being added to the object after the existing keys, and they're keys are the index number. How can insert the values in the existing keys instead?

    const memories = {
        email: '',
        date: '',
        relation: '',
        tips: '',
        location: '',

    };

    const backend = (e) => {
        e.preventDefault();
        const allInputs = Array.from(document.querySelectorAll('input'));
        const allValues = allInputs.map(input => input.value);
      
        for (const [index, value] of allValues.entries(){
            memories[index] = value;
        }
        console.log(memories);
    };
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You're retrieving the input values as an array, so you don't have any information about the names of the form fields.

You probably want something like:

const allInputs = Array.from(document.querySelectorAll('input'));
const allValues = allInputs.map(({name, value}) => ({name, value}));
  
for (const {name, value} of allValues) {
    memories[name] = value;
}

Complete snippet:

const memories = {
    email: '',
    date: '',
    relation: '',
    tips: '',
    location: '',

};

const allInputs = Array.from(document.querySelectorAll('input'));
const allValues = allInputs.map(({name, value}) => ({name, value}));
  
for (const {name, value} of allValues) {
    memories[name] = value;
}

console.log(memories);
<input name="email" value="test@example.com">
<input name="location" value="World">

about 4 years ago · Juan Pablo Isaza Relatório

0

See working example at CodeSandbox.

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <input type="text" name="email" value="a" />
    <input type="text" name="date" value="b" />
    <input type="text" name="relation" value="c" />
    <input type="text" name="tips" value="d" />
    <input type="text" name="location" value="e" />

    <button onclick="backend()">Submit</button>

    <script src="src/index.js"></script>
  </body>
</html>

index.js

const memories = {
  email: "",
  date: "",
  relation: "",
  tips: "",
  location: ""
};

window.backend = () => {
  const allInputs = Array.from(document.querySelectorAll("input"));

  allInputs.forEach((input) => {
    memories[input.name] = input.value;
  });

  console.log(memories);
};
about 4 years ago · Juan Pablo Isaza Relatório

0

Set the a name attribute for each input. The document.querySelectorAll() method returns a NodeList, and you can iterate it with for..of. Destructure the name and value in the the for..of loop, and update the object.

const memories = {
  email: '',
  date: '',
  relation: '',
  tips: '',
  location: '',
};
  
for (const { name, value } of document.querySelectorAll('input')) {
  memories[name] = value;
}

console.log(memories);
<input name="email" value="test@example.com" />
<input type="text" name="date" value="1.1.1970" />
<input type="text" name="relation" value="father" />
<input type="text" name="tips" value="tip" />
<input name="location" value="World" />

You can also use the NodeList.forEach() method instead of for..of:

const memories = {
  email: '',
  date: '',
  relation: '',
  tips: '',
  location: '',
};

document.querySelectorAll('input')
  .forEach(({ name, value }) => {
    memories[name] = value;
  });
  
console.log(memories);
<input name="email" value="test@example.com" />
<input type="text" name="date" value="1.1.1970" />
<input type="text" name="relation" value="father" />
<input type="text" name="tips" value="tip" />
<input name="location" value="World" />

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