How do you check to see if a specific tweet is retweeted using JavaScript or JQuery when displaying a twitter feed? [duplicate]

StackOverflow https://stackoverflow.com/questions/14180799

Question

Possible Duplicate:
Telling Whether A Tweet Is A Retweet Or Not?

So I've been working on a twitter feed script that pulls in my feed. I'm wondering how would I go about checking to see if a specific tweet is retweeted, and then if it is retweeted I want to use the information from the retweeted tweet instead. Here's an example associative array of what the twitter API produces for a retweet.

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
            ...
        )

)

So essentially I want to replace [text] => RT @Fullscreen: Heads-up: many... with [text] => Heads-up: many... only if it exists. If it doesn't exist then use the normal method of pulling the information from the tweets. I guess basically what I'm trying to get at is how do I check to see if [retweeted_status] exists then pull that information if not then pull the other information. I would like to avoid having the RT in there since it does appear to be possible and to use the information like id_str, text, screen_name, etc.

The code I use to get the information for the regular tweet pulling is:

id = tweets[i]["id_str"];
twitPic = tweets[0]["user"]["profile_image_url_https"];
content = tweets[i]["text"];
tweetTime = tweets[i]["created_at"];

where i is replaced by the index in a for loop. If you have any questions about what I'm trying to get at then let me know and I'll be happy to clear it up.

Was it helpful?

Solution

You can test if the key exists like this:

// 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"];
}

You could also checking for undefined, but this is not an accurate way of testing whether a key exists (What if the key exists but the value is actually undefined?).

OTHER TIPS

// save current tweet
var tweet = tweets[i];
var id = tweet.id_str;
var twitPic = tweet.user.profile_image_url_https;
var tweetTime = tweet.created_at;
var isRetweet = Object.prototype.toString.call(tweet.retweeted_status).slice(8, -1) === 'Object';
// try to use text from original tweet
var content = isRetweet ? tweet.retweeted_status.text : tweet.text;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top