2016-11-21 54 views
0

如何通過與字典詞典列表循環和內獲取值 - 我的Javascript,爲Ajax調用在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和字典,但是我無法訪問循環中所需的字典值。 ?如果有人能給出一個代碼示例,我將不勝感激。

謝謝

+0

什麼是錯誤 – Mahi

+0

@Mahi,我只是沒有得到價值,它說undefined –

+0

看到jkong的答案。它會工作 – Mahi

回答

1

在你循環,只是改變MYLIST [I]到MYLIST [I] [田],

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>") 
          } 
+0

您好我已經嘗試過,它說它不能讀取未定義...有我在檢查的thr列表中的數據,只是沒有在這個階段的想法 –

+0

@CuriousLambda你檢查你的名單嗎?你的名單的名字是'我的名單',我的名單和名單之間有一個空格,刪除這個空格,然後再試一次 – JKong

+0

這只是在堆棧溢出,這是正確的js文件。這是一個Django查詢集的序列化數據,它是基於js函數...謝謝 –

相關問題