如何将数组中的数组内容添加到html表?

时间:2020-03-05 16:04:45

标签: javascript arrays

我有一个功能,可以从博客文章数据库中请求数据,并且要求我显示谁喜欢这些文章。我设法用搜索结果设置了一个数组,但是当我尝试登录到控制台时,我返回了“ undefined”,并且如果我尝试将数组添加到数组中,我返回了[对象]的html表。

var filResponse = [];

function getLikes(){
 $.ajax({
  url: "site/_api/web/lists/getbytitle('Posts')/items?$select=LikesCount,LikedBy/Title&$expand=LikedBy",
  type: "GET",
  headers: {"accept": "application/json;odata=verbose"},
  success: function(data) {
   filResponse = data.d.results;
   console.log(filResponse);
   var results = document.getElementById("results");
   for(var obj in filResponse){
    if(filResponse[obj].LikesCount){
     var likedBy = filResponse[obj].LikedBy.results;
     console.log(likedBy.Title);
     results.innerHTML += "<tr><td>" + likedBy.Title + "</td></tr>";
    }
   }
  }
 });
}
"filResponse": [
{"LikedBy": "Results":[
  {"Title": "Adam Apple"},
  {"Title": "Betty Banana"},
  {"Title": "Charlie Carrot"}
  ],
 "LikesCount": 3
 }
{"LikedBy": "Results":[
  {"Title": "Adam Apple"},
  {"Title": "Charlie Carrot"}
  ],
 "LikesCount": 2
 }
]

不确定如何在数组中显示JSON数组,但是我添加了一个屏幕快照,显示了当我完整记录该数组时其在控制台中的显示方式:

enter image description here

0 个答案:

没有答案