Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

javascript - how to access [object HTMLTableCellElement]

Is there a way to make [object HTMLTableCellElement] into a string to use it in my HTML site?

Have tried .innerHTML, .toString()

Console.log prints it out without issues, only when trying to implement into HTML it comes out as [object HTMLTableCellElement]

question from:https://stackoverflow.com/questions/65943947/how-to-access-object-htmltablecellelement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code has some problems with asychronous loading. And you are also going to append things out of order. Personally I would use fetch and promise all

function displayRestaurants() {
  var table = document.getElementById("restaurantTable");
  table.innerHTML = "";

  var promises = restaurant_array.map(function(restaurant) {
    return fetch('/averagepriceratings/' + restaurant._id)
      .then(function(resp) {
        return resp.json();
      }).then(function(json) {
        var average = json[0]['avg(priceRating)']
        var logo = restaurant.logo;
        var name = restaurant.name;
        var tags = [restaurant.priceCategory, restaurant.cuisineCategory, restaurant.areaCategory].join(', ');
        var cell = "<div>" + tags + "</div>";
        return cell
      });
  });

  Promise.all(promises).then(function(htmlStrs) => {
    table.innerHTML = htmlStrs.join('');
  });

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...