[英]Angular4 loop through returned JSON object in .subscribe using typescript
[英]Loop through JSON object returned from cookie
我知道對此有很多問題,但是我嘗試了幾種不同的提議解決方案,並且每個解決方案都得到了相同的結果。
我將復選框值以timeArr = [ event-name-one: 0: 1404915600, 1: 1404917400 ]
的格式存儲在數組中。 當使用新的鍵/值對更新數組時,將更新cookie,該cookie將數組存儲為JSON對象。
我正在使用jQuery.cookie('day', JSON.stringify(timeArr), {expires: 7});
將JSON對象存儲在cookie中jQuery.cookie('day', JSON.stringify(timeArr), {expires: 7});
它以以下格式存儲數組(從console.log();
返回):
{"event-name-one":{"0":1405346400,"1":1405347600},"event-name-two":{"0":1405357200,"1":1405358400}}
在這種情況下, event-name-one
和event-name-two
是復選框的ID。 我需要遍歷返回的cookie值(JSON對象),並選中在返回的cookie中找到誰的ID的復選框。
我嘗試了幾種不同的循環,即for(var k in cookieValue){}
和jQuery.each(jQuery.parseJSON(cookieValue), function(i, v) {});
沒有運氣。
for(var k in cookieValue)
循環分別返回對象的每個字母,而jQuery.each()
循環返回此錯誤: Cannot use 'in' operator to search for '76' in ...
我如何將這個JSON字符串轉換回數組,以便可以遍歷它並獲取“鍵”
jQuery.cookie()
必須將數組自動字符串化為JSON對象,因為為了從循環中獲取正確的結果,我不得不在響應上使用jQuery.parseJSON()
兩次。
例:
var cookieVal = jQuery.cookie('day_planner');
jQuery.each(jQuery.parseJSON(jQuery.parseJSON(cookieVal)), function(i, v) {
console.log(i, v);
});
此循環返回event-name-one Object {0: "1405346400", 1: "1405347600"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.