Possible Duplicate:
Telling Whether A Tweet Is A Retweet Or Not?
所以我一直在研究一个提取我的提要的 Twitter 提要脚本。我想知道如何检查特定推文是否被转发,然后如果被转发,我想使用转发的推文中的信息。下面是 twitter API 为转发生成的关联数组示例。
Array
(
[0] => Array
(
[created_at] => Sun Jan 06 08:28:39 +0000 2013
[id] => 287838076443705345
[id_str] => 287838076443705345
[text] => RT @Fullscreen: Heads-up: many YT partners are reporting issues claiming their videos. It appears to be a YT bug. We will keep you posted.
[source] => web
...
[user] => Array
(
[id] => 212642904
[id_str] => 212642904
[name] => Elias Ranz-Schleifer
[screen_name] => Xxplosions
...
)
...
[retweeted_status] => Array
(
[created_at] => Sun Jan 06 02:08:43 +0000 2013
[id] => 287742460824805378
[id_str] => 287742460824805378
[text] => Heads-up: many YT partners are reporting issues claiming their videos. It appears to be a YT bug. We will keep you posted.
...
[user] => Array
(
[id] => 238110322
[id_str] => 238110322
[name] => Fullscreen, Inc.
[screen_name] => Fullscreen
...
)
...
)
[retweet_count] => 44
...
)
)
所以本质上我想替换 [text] => RT @Fullscreen: Heads-up: many...
与 [text] => Heads-up: many...
仅当它存在时。如果不存在,则使用从推文中提取信息的常规方法。我想基本上我想要了解的是如何检查 [retweeted_status]
是否存在则提取该信息,如果不存在则提取其他信息。我想避免 RT 在那里,因为它看起来确实是可能的,并使用像 id_str, text, screen_name, etc.
这样的信息。
我用来获取常规推文拉取信息的代码是:
id = tweets[i]["id_str"];
twitPic = tweets[0]["user"]["profile_image_url_https"];
content = tweets[i]["text"];
tweetTime = tweets[i]["created_at"];
哪里i
被 for 循环中的索引替换。如果您对我想要了解的内容有任何疑问,请告诉我,我很乐意为您解答。
最佳答案
您可以像这样测试 key 是否存在:
// by using the 'in' operator
content = ('retweeted_status' in tweets[i]) ? tweets[i]["retweeted_status"] : tweets[i]["text"];
// or by using the method 'hasOwnProperty()'
if(tweets[i].hasOwnProperty('retweeted_status')) {
content = tweets[i]["retweeted_status"];
} else {
content = tweets[i]["text"];
}
您还可以检查未定义,但这不是测试键是否存在的准确方法(如果键存在但值实际上未定义怎么办?)。
关于javascript - 在显示 twitter feed 时,如何使用 JavaScript 或 JQuery 检查特定推文是否被转发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180799/