最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Displaying List Of JavaScript Objects As HTML List Items - Stack Overflow

programmeradmin0浏览0评论

When I attempt this, the HMTL page only displays the last object, instead of all the objects.

Here is my JavaScript file

var family = {
  aaron: {
    name: 'Aaron',
    age: 30
  },
  megan: {
    name: 'Megan',
    age: 40
  },
  aaliyah: {
    name: 'Aaliyah',
    age: 2
  }
}

var list = function(family) {
  for (var prop in family) {
    var elList = document.getElementById('aaron-family').innerHTML = prop;
  }
}

list(family);

And here's my HTML file

<html>
<head>
  <title>Aaron's Test With Objects</title>
</head>
  <li id="aaron-family">Hey, this is where it goes</li>
<footer>
  <script src="objects.js"></script>
</footer>
</html>

When I attempt this, the HMTL page only displays the last object, instead of all the objects.

Here is my JavaScript file

var family = {
  aaron: {
    name: 'Aaron',
    age: 30
  },
  megan: {
    name: 'Megan',
    age: 40
  },
  aaliyah: {
    name: 'Aaliyah',
    age: 2
  }
}

var list = function(family) {
  for (var prop in family) {
    var elList = document.getElementById('aaron-family').innerHTML = prop;
  }
}

list(family);

And here's my HTML file

<html>
<head>
  <title>Aaron's Test With Objects</title>
</head>
  <li id="aaron-family">Hey, this is where it goes</li>
<footer>
  <script src="objects.js"></script>
</footer>
</html>
Share Improve this question asked Jan 31, 2016 at 0:08 Aaron KantrowitzAaron Kantrowitz 1011 gold badge1 silver badge5 bronze badges 1
  • You need to append new items to the list. – PM 77-1 Commented Jan 31, 2016 at 0:13
Add a comment  | 

5 Answers 5

Reset to default 7

Thank you guys! Here's the final solution I came up with:

JS

var family = {
  aaron: {
    name: 'Aaron',
    age: 30
  },
  megan: {
    name: 'Megan',
    age: 40
  },
  aaliyah: {
    name: 'Aaliyah',
    age: 2
  }
}

var list = function(family) {
  for (var prop in family) {
    document.getElementById('aaron-family').innerHTML += '<li>' + prop + '</li>';
    console.log(prop);
  }
}

HTML

<html>
<head>
  <title>Aaron's Test With Objects</title>
</head>
<ul id="aaron-family">
</ul>
<footer>
  <script src="objects.js"></script>
</footer>
</html>

I'm sure it can be refactored but it works, visually.

Well, you've got a couple problems there (<li> tag without a parent <ol> or <ul> tag, among others)...but I'd say the primary error is that you are replacing each subsequent output with each assignment to innerHTML.

Solution: assign a compiled array to innerHTML (using join to include spaces between the values)

var list = function(family) {
  var names = [];
  for (var prop in family) {
    names.push(prop.name);
  }
  document.getElementById('aaron-family').innerHTML = names.join(' ');
}
list(family);

Remove elList because there is no point in having it...

Then change

document.getElementById('aaron-family').innerHTML = prop;

To

document.getElementById('aaron-family').innerHTML += prop;

That way you are not constantly setting the innherHTML to prop. Also, you might find it better to change the function to the following in order to prevent from constantly getting the element.

function list(family) {
  var elList = document.getElementById('aaron-family');
  for (var prop in family) {
    elList.innerHTML += prop;
  }
}

Hope this helps:)

This dynamic way to render like this data format into HTML

const elList = document.getElementById('aaron-family')

function convertObjectsToArrayOfObjects(family) {
    const filteredData = []
    for(person in family){
          filteredData.push(family[person])
     }
    return filteredData;
 }

function elList(family) {
   const familyData = convertObjectsToArrayOfObjects(family)
   elList.innerHTML = `
      ${
        familyData.map(person => {
          return `
            <h1>name: ${person.name}</h1>
            <h2>age: ${person.age}</h2>
          `   
       })
     }
  `
}

list(family);

Just you don't need to define function

html file:

var family = {
    aaron: {
        name: 'Aaron',
        age: 30
    },
    megan: {
        name: 'Megan',
        age: 40
    },
    aaliyah: {
        name: 'Aaliyah',
        age: 2
    }
}

for (var prop in family) {

    document.getElementById('aaron-family').innerHTML += '<li>' + prop + '</li>';

}
<html>
    <head>
        <title>Aaron's Test With Objects</title>
    </head>
    <body>

        <ul id="aaron-family">
        </ul>
        <script src="objects.js" type="text/javascript"></script>

    </body>
</html>

发布评论

评论列表(0)

  1. 暂无评论