2013-03-14 3 views
0

일부 실시간 스트리밍 JSON 데이터를 구문 분석하고 이벤트에 특정 태그가 있는지 확인하려고합니다. 그 다음 I이 어떤 이유 등자바 스크립트 함수에서 JSON 객체 반환

출력값 데이터를 사용할 것이다 없으면 , upcoming_eventfindPublicEvent 함수의 리턴 값 이벤트 객체 (할당되는 것은 아니다.

이벤트 객체의

을 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. 익명 함수 내에서 값을 할당
  2. 반환 (하지 반환 정기적 할당을 사용) findPublicEvent
  3. 의 범위에서 변수를 정의하는 변수 findPublicEvent
+0

에서 도와 주셔서 대단히 감사합니다 @ 퀸틴! 매력처럼 작동합니다 : D – watts

관련 문제