Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!