Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda