jquery - Ajax response not fetching all result
问题描述
I am trying to fetch some results from database and display them to a table. I am getting the full response which is as below,
Response payload
{"groupname":
[
{
"id":12,"group_name":"sd"
},
{
"id":11,"group_name":"ghf"
},
{
"id":10,"group_name":"sdf"
},
{
"id":9,"group_name":"dsf"
},
{
"id":5,"group_name":"g"
},
{
"id":4,"group_name":"too"
},
{
"id":3,"group_name":"lol"
},
{
"id":2,"group_name":"hh"
},
{
"id":1,"group_name":"TestGroup"}
],
"question_count":
[
{
"count":1
},
{
"count":3
},
{
"count":1
},
{
"count":6
},
{
"count":1
},
{
"count":5
}
],
"img_url":"http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/icn_close.png",
"drag_img_url":"http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/drag-handle.png"
}
I displayed them into a table by the below code
for (var i = 0; i <= Object.keys(data).length; i++) {
if (data.groupname[i].group_name) {
txt = '<tr class="row1" data-id="' + data.groupname[i].id + '"><td>' + data.groupname[i].group_name + '</td>';
}
$("#table").append(txt);
}
But I am getting only limited groupnames, but there are total 9 group names in the result. When I alert Object.keys(data).length it gives me only 4. So I need to loop through the whole groupnames. Thanks in advance.
解决方案
您正在4
从下面的代码中获取长度,因为里面有 4 个对象,data
它们是groupname
、和。question_count
img_url
drag_img_url
Object.keys(data).length
如果你想遍历组,你应该这样做:
Object.keys(data.groupname).length
其次,自从您<=
使用.<
length
正如我在对原始帖子的评论中提到的那样,因为groupname
是一个数组,您可以使用length
它并进行迭代。
var data = {
"groupname": [{ "id": 12, "group_name": "sd" }, { "id": 11, "group_name": "ghf" }, { "id": 10, "group_name": "sdf" }, { "id": 9, "group_name": "dsf" }, { "id": 5, "group_name": "g" }, { "id": 4, "group_name": "too" }, { "id": 3, "group_name": "lol" }, { "id": 2, "group_name": "hh" }, { "id": 1, "group_name": "TestGroup" }],
"question_count": [{ "count": 1 }, { "count": 3 }, { "count": 1 }, { "count": 6 }, { "count": 1 }, { "count": 5 }], "img_url": "http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/icn_close.png", "drag_img_url": "http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/drag-handle.png"
};
for (var i = 0; i < data.groupname.length; i++) {
if (data.groupname[i].group_name) {
txt = '<tr class="row1" data-id="' + data.groupname[i].id + '"><td>' + data.groupname[i].group_name + '</td>';
}
console.log(txt);
}
更短的语法:
var data = {
"groupname": [{ "id": 12, "group_name": "sd" }, { "id": 11, "group_name": "ghf" }, { "id": 10, "group_name": "sdf" }, { "id": 9, "group_name": "dsf" }, { "id": 5, "group_name": "g" }, { "id": 4, "group_name": "too" }, { "id": 3, "group_name": "lol" }, { "id": 2, "group_name": "hh" }, { "id": 1, "group_name": "TestGroup" }],
"question_count": [{ "count": 1 }, { "count": 3 }, { "count": 1 }, { "count": 6 }, { "count": 1 }, { "count": 5 }], "img_url": "http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/icn_close.png", "drag_img_url": "http:\/\/localhost\/O2X\/Trunk\/Web\/public\/admin\/images\/drag-handle.png"
};
data.groupname.forEach(token => {
txt = '<tr class="row1" data-id="' + token.id + '"><td>' + token.group_name + '</td>';
console.log(txt);
});
注意:我已txt
在控制台上打印,而不是附加到表格中进行演示。
推荐阅读
- azure - Terraform azure - keyvault key generation - access denied
- angular - 如何在 MatTable 中显示键值条件数据?
- r - 是否有一个 R 函数可以为一组变量的每个值减去不同的中位数?
- php - 在php中修剪电子邮件字段
- google-classroom - 如何使用 Google 课堂 API 将班级的所有权转让给没有原始教师帐户的其他教师
- kivy - 当我尝试添加标签时,Kivy 返回错误
- sql-server - 在兼容模式下转换旧的 SQL 数据库
- sql - Optimization needed for insert into query with select having self join
- algorithm - 贪心在线装箱算法
- sql - Left join brainfart