2016-03-04 134 views
0

我想顯示開口小時,一個註冊用戶的註冊時填寫在一週價值的缺失天填寫:jQuery的 - 從對象

這是「value」 &它以這種方式存儲在DB:

[ 
{"day":"Maandag","daylabel":"Maandag","fromtime":"15:30","fromtimelabel":"3:30 pm","totime":"22:00","totimelabel":"10:00 pm"}, 
{"day":"Dinsdag","daylabel":"Dinsdag","fromtime":"15:30","fromtimelabel":"3:30 pm","totime":"22:00","totimelabel":"10:00 pm"}, 
{"day":"Donderdag","daylabel":"Donderdag","fromtime":"15:30","fromtimelabel":"3:30 pm","totime":"22:00","totimelabel":"10:00 pm"}, 
{"day":"Vrijdag","daylabel":"Vrijdag","fromtime":"15:30","fromtimelabel":"3:30 pm","totime":"22:00","totimelabel":"10:00 pm"}, 
{"day":"Zaterdag","daylabel":"Zaterdag","fromtime":"09:30","fromtimelabel":"9:30 am","totime":"+00:00","totimelabel":"12:00 am (next day)"}, 
{"day":"Zondag","daylabel":"Zondag","fromtime":"09:30","fromtimelabel":"9:30 am","totime":"+00:00","totimelabel":"12:00 am (next day)"} 
] 

這是我的函數解析value

config.profile.core.parseAvailability = function(value){ 
    if(value){ 
    try { 
     var availabilityDays = JSON.parse(value); 
     $jq.each(availabilityDays, function(index, day) { 
      $jq('#availability').append('<div class="col-sm-5">'+day['daylabel']+'</div><div class="col-sm-7">'+day['fromtime']+' - '+day['totime']+'</div>'); 
     }); 
    } catch (error) { 
     console.error(error); 
    } 
    } 
}; 

這是它會顯示在p的方式年齡:

enter image description here

問題:

正如你或許可以看到,週三(在 'dinsdag' & 'donderdag'之間的荷蘭人 'woensdag' )當天丟失,因爲它並非故意填充(可能會在特定的一天關閉)。因爲那一天沒有儲存,我無法顯示它。

我該如何顯示缺少空白值的日子?

回答

2

而不是遍歷數據你已經,環比數據,你然後顯示什麼與它匹配

var days = ['Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag']; 
var availabilityDays = JSON.parse(value); 

for (var day of days) { 
    var details = availabilityDays.find(function (detail) { 
     return detail.day == day; 
    }); 

    var detailsText = ''; 

    if (details) { 
     detailsText = details['fromtime']+' - '+details['totime'] 
    } 

    $jq('#availability').append('<div class="col-sm-5">'+day+'</div><div class="col-sm-7">' + detailsText + '</div>'); 
} 
+0

非常感謝你。 – Mark