Displaying List Of JavaScript Objects As HTML List Items

前端 未结 5 1119
野趣味
野趣味 2021-01-23 17:58

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: {
    n         


        
5条回答
  •  不知归路
    2021-01-23 18:24

    Well, you've got a couple problems there (

  • tag without a parent
      or
        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);
        

提交回复
热议问题