0

Hello there am trying to save news tweets into three different array which are dynamically created.

am finding trouble when i want to get the text from each one of those array and make another request to twitter.

    news_tweets("reuters","1652541",3);
    function news_tweets(query, user_id,count) {
        news_array = [];
        $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=false&user_id=" + user_id + "&count="+count+
        "&callback=?",
        function (data) {
      for (var i = 0; i < count; i++) {
          var user = data[i].user.name;
          var date = data[i].created_at;
          var profile_img = data[i].user.profile_image_url;
          var text = data[i].text;
          var url = (data[i].entities.urls.length > 0 ? data[i].entities.urls[0].url : '');
          news_array[i] = [{user:user,date:date,profile_img:profile_img,text:text,url:url}];
        }  
      for (var i = 0; i < news_array.length; i++) {
          for (var x=0; x<i.length; x++){
              console.log(news_array[i][x].user);
              }
            }
        });
    }

It doesn't show anything on the console.log.

thanks for the help!!!!!

anjel
  • 1,355
  • 4
  • 23
  • 35

4 Answers4

1

First, make sure that your count is smaller than the data array's length, otherwise this could lead to some undefined values:

for (var i = 0; i < count && i < data.length; i++) …

Then, why are you creating all those one-element-arrays in the news_array? Just use only objects.

This would solve your actual issue: You are looping wrong over those inner arrays. The correct code would be

for (var i = 0; i < news_array.length; i++) {
    for (var x = 0; x < news_array[i].length; x++){
        console.log(news_array[i][x].user);
    }
}

Also, you should indent your code properly. You have some odd braces around, which don't make the code readable.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Bergi can you help me with this one? http://stackoverflow.com/questions/12446776/request-twitter-api-without-entering-a-loop – anjel Sep 16 '12 at 13:00
0

The problem is the x<i.length in the for loop near the end. i is a number, so it doesn't have a length. You probably meant x < news_array[i].length.

Abraham
  • 20,316
  • 7
  • 33
  • 39
0

You may try the following:

  • Use the push method to append elements / data in your array new_array

  • Use only 1 loop for to display the user value on console

So your code will be something like this:

news_tweets("reuters","1652541",3);

function news_tweets(query, user_id,count) {
    news_array = [];
    $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=false&user_id=" + user_id + "&count="+count+
    "&callback=?",
        function (data) {
          for (var i = 0; i < count; i++) {
              var user = data[i].user.name;
              var date = data[i].created_at;
              var profile_img = data[i].user.profile_image_url;
              var text = data[i].text;
              var url = (data[i].entities.urls.length > 0 ? data[i].entities.urls[0].url : '');

              // Pushing your elements in your array, 1 by 1
              news_array.push({user:user,date:date,profile_img:profile_img,text:text,url:url});
        }  

        // Here you only need 1 loop!
        for (var i = 0; i < news_array.length; i++) {
            console.log(news_array[i][x].user);
        }
    });
}
Littm
  • 4,923
  • 4
  • 30
  • 38
0

First thing is i would loop the first one till data.length rather than count because its an api and it "might" or "might not" return all the data. So it will be fool proof to loop till data.length

And your problem is with i.length

for (var i = 0; i < news_array.length; i++) { 
    console.log(news_array[i].user);
}

this should work. not sure why you had to loop through a loop.

Srinivas
  • 727
  • 4
  • 14