![](/img/trans.png)
[英]Looping through a dictionary in javascript with a list within the values
[英]How to loop through a list of dictionaries with in a dictionary and get the values within - Javascript, for an ajax call in django
我有一个清单
mylist = [{"model": "todolist.todolist", "pk": 1, "fields": {"timezoneChoice": "Africa/Johannesburg", "user": 44, "name": "ergdsg", "discription": "dsgdsg", "dueMinutes": -61504892, "emailSent": false, "dueDate": "1899-12-11T08:20:00Z", "create_at": "2016-11-21T04:37:29.253Z"}}, {"model": "todolist.todolist", "pk": 2, "fields": {"timezoneChoice": "Africa/Johannesburg", "user": 44, "name": "ergdsg", "discription": "dsgdsg", "dueMinutes": -61504892, "emailSent": false, "dueDate": "1899-12-11T08:20:00Z", "create_at": "2016-11-21T04:38:12.525Z"}}]
我正在尝试从“字段”及其键中获取值
for (var i = 0; i < mylist.length; i++) {
$("#posts").prepend( "<div >" +"<h2 class='bg-info text-align-center'> Tasks" +"</h2>"
+ "<strong> Name: " + " " + mylist[i].name + "</strong>"
+ "<strong> Task: " + " " + mylist[i].discription + "</strong>"
+ "<strong> Due Date: " + " " + mylist[i].dueDate + "</strong>"
+ "<strong> Mins before due: " + " " + mylist[i].dueMinutes + "</strong>"
+ "<strong> TimeZone: " + " " + mylist[i].timezoneChoice + "</strong>"
+ "<hr>" +"</div>")
},
我该怎么做。 请我真的需要帮助,并且整日都在挣扎。 我已经阅读了有关javascript和字典的内容,但是在循环中无法访问所需的字典值。 ? 如果有人可以提供代码示例,我将不胜感激。
谢谢
在您的循环中,只需将mylist [i]更改为mylist [i] [fields'],
for (var i = 0; i < mylist.length; i++) {
$("#posts").prepend( "<div >" +"<h2 class='bg-info text-align-center'> Tasks" +"</h2>"
+ "<strong> Name: " + " " + mylist[i]['fields'].name + "</strong>"
+ "<strong> Task: " + " " + mylist[i]['fields'].discription + "</strong>"
+ "<strong> Due Date: " + " " + mylist[i]['fields'].dueDate + "</strong>"
+ "<strong> Mins before due: " + " " + mylist[i]['fields'].dueMinutes + "</strong>"
+ "<strong> TimeZone: " + " " + mylist[i]['fields'].timezoneChoice + "</strong>"
+ "<hr>" +"</div>")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.