我有一个清单
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和词典的内容,但我无法在循环中访问所需的字典值。 ?如果有人能给出代码示例,我将非常感激。
谢谢
答案 0 :(得分:1)
在你的循环中,只需将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>")
}