Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:11:32+00:00 2026-06-11T11:11:32+00:00

Anyone knows how can i make requests to twitter api based on text queries

  • 0

Anyone knows how can i make requests to twitter api based on text queries without using a recursion.

this is my code

        function news_tweets(query, user_id, count) {
            news_array = [];
            user_tweets = [];
            full_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) {
                $.each(data, function (i, item) {
                    var user = item.user.name;
                    var date = item.created_at;
                    var profile_img = item.user.profile_image_url;
                    var text = item.text;
                    var url = (item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
                    news_array.push({
                        news_user: user,
                        news_date: date,
                        news_profile_img: profile_img,
                        news_text: text,
                        news_url: url
                    });
                });
                find_tweets(news_array);

            });
        }

        function find_tweets(news_array) {
            for (var i in news_array) {
                var news_text = news_array[i].news_text;
                $.getJSON("http://search.twitter.com/search.json?q=" + news_text + 
                "&rpp=10&include_entities=true&result_type=mixed&callback=?",

                function (data) {
                    $.each(data.results, function (i, item) {
                        var user = item.from_user;
                        var user_id = item.from_user_id;
                        var date = item.created_at;
                        var user_profile_img = item.profile_image_url;
                        var text = item.text;
                        var url = (item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
                        user_tweets.push({
                            user: user,
                            user_id: user_id,
                            date: date,
                            user_profile_img: user_profile_img,
                            text: text
                        });
                    });
                    combine_arrays(news_array, user_tweets);
                });
            }

            function combine_arrays(news_array, user_tweets) {
                full_array = news_array.concat(user_tweets); console.log(full_array);
                }

             }  

when i use console.log(“hello”) or try to connect the two arrays everything is executed three times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:11:33+00:00Added an answer on June 11, 2026 at 11:11 am

    You seem to have only one instance of the news_array and user_tweets arrays. On those, you push all the result of your api queries. Yet, you call the combine_arrays function on the whole arrays from a loop (each time after the search gave you a new set of results) – running multiple times over some of the items.

    I guess re-initializing

    var user_tweets = [];
    

    inside the find_tweets function would help something.


    You can’t access the ajax data outside the callback. Instead, you will need to wait until all the asynchronous requests are resolved. I recommend to use jQuery’s Deferred object which makes handling such things much easier:

    function news_tweets(query, user_id, count) {
        var news_array = [],
            user_tweets = [];
        return $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json", {
            include_entities: "true",
            include_rts: "false",
            user_i: user_id,
            count: count
        }).then(function (data) {
            return $.when.apply(null, $.map(data, function (item) {
                news_array.push({
                    news_user: item.user.name,
                    news_date: item.created_at,
                    news_profile_img: item.user.profile_image_url,
                    news_text: item.text,
                    news_url: item.entities.urls.length ? item.entities.urls[0].url : ''
                });
                return $.getJSON("http://search.twitter.com/search.json", {
                    q: item.text,
                    rpp: 10,
                    include_entities: "true",
                    result_type: "mixed"
                }).done(function (data) {
                    $.each(data.results, function (i, item) {
                        user_tweets.push({
                            user: item.from_user,
                            user_id: item.from_user_id,
                            date: item.created_at,
                            user_profile_img: item.entities.urls.length ? item.entities.urls[0].url : '',
                            text: item.text
                        });
                    });
                });
            }));
        }).then(function() {
            // this callback is executed [once] when all requests are done
            // and the user_tweets array is filled
            // arguments is an array of all search request results
            var full_array = news_array.concat(user_tweets);
            console.log(full_array);
            return full_array;
        })
    }
    

    Usage:

    news_tweets(…).done(function callback(full_array) {
        // do something with all the objects
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone knows where I can find tutorials and code samples, basic and advanced,
Does anyone know I can make min-height work with the latest browsers? I am
Anyone know how I can make a login button in Ruby on Rails for
Does anyone know how you can make a cocoa sheet with rounded corners like
Anyone knows how i can implement re-arrangable divs (Drag the divs around on the
Does anyone knows how i can get buyer's reviews from a Product in a
Hi i was wondering if anyone knows how i can calculate the difference between
Have a rather simple question. Does anyone knows if i can use jparallax both
I'm clearly a newb, and I was wondering if anyone knows how I can
does anyone knows any good resources and articles that can explain how to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.