2013-03-14 318 views
0

嘗試解析某些直播流JSON數據並查看事件是否具有特定標記。 如果它不那麼我會用這些數據來輸出值等從Javascript函數返回JSON對象

無論出於何種原因,upcoming_event沒有被指派的事件對象(這是findPublicEvent函數的返回值。

事件對象的執行console.log工作正常 - 但它返回不工作:/

// get our NLC data from livestream. 
// -> note: need the '?callback=?' to convert to JSONP for cross-domain usage 
var $uri = 'http://api.new.livestream.com/accounts/newlifechurchtv/?callback=?'; 
$.getJSON($uri, function(data) { 
    parseNLCData(data); 
}); 

parseNLCData = function(nlc_data){ 
    // set our variable to the return first event 
    // nlc_data.upcoming_events.data is a json array of events 
    window.upcoming_event = findPublicEvent(nlc_data.upcoming_events.data); 
} 

// should return single public event 
function findPublicEvent (all_events) { 
    // if we have events 
    if (all_events) { 
    // loop through events to find public event 
    $.each(all_events, function(index,value){ 
     // get all the tags, remove whitespace, and put into array 
     var $tags = value.tags.replace(/ /g, '').toLowerCase().split(','); 
     // check for privacy. 
     var $privacy = $.inArray('private', $tags); 
     if ($privacy === -1) { 
     // if the event isn't private -> return it! 
     console.log(value); 
     return value; 
     } 
    }); 
    // otherwise .... -> 
    } else { 
    // we don't have events, sooo, no dice. 
    return false; 
    } 

}; 

回答

3

findPublicEvent沒有返回它傳遞給each匿名函數返回它

由於它是您捕獲的findPublicEvent的返回值,因此您無法看到它。

  1. 定義爲findPublicEvent
  2. 範圍的變量從您的匿名函數中值分配給它(使用普通的分配,不是迴歸)
  3. 返回變量從findPublicEvent
+0

非常感謝您的幫助@Quentin!像魅力一樣工作:D – watts 2013-03-14 17:59:11