gpt4 book ai didi

javascript - 从 Javascript 函数返回 JSON 对象

转载 作者:行者123 更新时间:2023-11-28 20:35:45 24 4
gpt4 key购买 nike

尝试解析一些直播 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;
}

};

最佳答案

findPublicEvent 未返回它。您传递给 each 的匿名函数将返回它。

由于您正在捕获的是 findPublicEvent 的返回值,因此您看不到它。

  1. findPublicEvent范围内定义变量
  2. 从匿名函数内部为其赋值(使用常规赋值,而不是返回)
  3. findPublicEvent 返回该变量

关于javascript - 从 Javascript 函数返回 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com