0

I'm Trying to make a list display in HTML of the stored details i put in my object using Javascript. which should look like this

name: Tester1 , age: 1
name: Tester2 , age: 2
name: Tester3 , age: 3

I used for in loop to display all the inputted objects, but it this happened

Actual:
name: Tester1 , age: 1name: Tester1 , age: 1name: Tester2 , age: 2name: Tester1 , age: 1name: Tester2 , age: 2name: Tester3 , age: 3

Here is the code for the Jascript

var person = [];
function profile(e) {
    //to stop form from resubmitting
    e.preventDefault(); 

    var pProfile = {
        name: document.getElementById('name').value,
        age: document.getElementById('age').value
    }
    person.push(pProfile);
    //used to convert result to string
    var myJSON = JSON.stringify(person);

    document.forms[0].reset();

    //display in Final Obejct div 
    document.getElementById("fObject").innerHTML = myJSON;

    //display converted objects to
    for (let x in person){
         document.getElementById("rFormat").append("name: " + person[x].name + " , " + "age : " + person[x].age + "<br>");
         console.log(person[x]);
    }
}

And this is for the HTML

    <div id="part1">
        <h3>Part 1</h2>
        <form id="profile" onsubmit="profile(event)" action="#">
            Name: <input type="text" id ="name">
            Age: <input type="text" id ="age">
            <input type="Submit" value="Add">           
        </form>

        <h4>Final Object</h4>
        <div id="fObject"></div>

        <h4>Readable Format</h4>
        <div id="rFormat"></div>
    </div>

Pretty sure that I did something wrong with the for in loop, but i don't know exactly where. Please help. I'm new in javascript. Thanks so much.

0

4 Answers 4

1

Instead of append() try with insertAdjacentHTML() or innerHTML. You also do not need the loop (since you are updating the HTML in each click), you can simply populate the last item in the array:

var person = [{name: "Tester1",age: 1 },{name: "Tester2",age: 2 },{name: "Tester3",age: 3 }];
var person = [];
function profile(e) {
  //to stop form from resubmitting
  e.preventDefault(); 

  var pProfile = {
      name: document.getElementById('name').value,
      age: document.getElementById('age').value
  }
  person.push(pProfile);
  //used to convert result to string
  var myJSON = JSON.stringify(person);

  document.forms[0].reset();

  //display in Final Obejct div 
  document.getElementById("fObject").innerHTML = myJSON;

  //display converted objects to
  var p = person[person.length-1]; //last item in the array
  document.getElementById("rFormat").innerHTML += "name: " + p.name + ", " + "age : " + p.age + "<br>";
}
<div id="part1">
  <h3>Part 1</h2>
  <form id="profile" onsubmit="profile(event)" action="#">
      Name: <input type="text" id ="name">
      Age: <input type="text" id ="age">
      <input type="Submit" value="Add">           
  </form>

  <h4>Final Object</h4>
  <div id="fObject"></div>

  <h4>Readable Format</h4>
  <div id="rFormat"></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

person is an array. use for of. Then use x directly.

for in is for objects. If you use it on an array, you will just get the index.

Comments

0

The correct way is by using the innerHTML method instead:

for (let x in person) {
    document.getElementById("rFormat").innerHTML += person[0] + "<br>";
}

I am not sure as to why yours does not work though.

Comments

0

You need to change the for-loop to use for-of instead of for-in. You need the values and not the keys

for(let x of person)

and then instead of person[x], you already have the object you need so use as below

.append("name: " + x.name + " , " + "age : " + x.age

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.