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 8273719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:33:23+00:00 2026-06-08T07:33:23+00:00

I have an array of Facebook users (userList) and I want to store the

  • 0

I have an array of Facebook users (userList) and I want to store the number of mutual friends for each user in the array as a property (mfCount). I have checked that I am getting the correct number of mutual friends if I put in an individual user, but I’m not sure why I can’t add this value to each user in the array?

function getfriends() {
FB.api('/me/friends', function(response) {
    userList = userList.concat(response.data);
    userCount = response.data.length;
    for( i=0; i<response.data.length; i++) {
      userId = response.data[i].id;
      FB.api('/me/mutualfriends/'+userId+'/', function(response) {
        userList[i].mfCount = response.data.length;
        userCount--;
        if(userCount === 0) { display_results();}
      });
    } 
  });

}

  • 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-08T07:33:24+00:00Added an answer on June 8, 2026 at 7:33 am

    Have a look at the implementation below.
    I’ve broken it out into multiple functions to separate each step.
    When you’re dealing with loops and callbacks, it becomes very important to keep track of what scope your anonymous functions are being defined in.

    You can theoretically do it all in a one-liner like you were writing…
    …but it gets very, very confusing as you go further and further into nested-callbacks.

    One solution would be to make every variable inside each function 100% global, so that only i needs to have an enclosed reference. That’s not really pretty, though.

    Look through each function and take note of what parameters are going into the functions each step calls (or closures for callbacks). They’re all needed (whether you separate them this way, or through closures in a one-liner or whatever).

    The following worked just fine for me, inside of the Facebook developer sandbox (first time using the API).
    The logs were for my benefit to see how the data was coming out, and to keep a basic stack-trace.

    var userList = [],
        userCount = 0;
    
    function getfriends () {
        //console.log("getFriends");
        var url = "/me/friends";
        FB.api(url, function (response) {
            if (response.error && response.error.message) { return false; }
            userList = userList.concat(response.data);
            userCount = response.data.length;
            compareAllFriends();
        });
    }
    
    function compareAllFriends () {
        //console.log("compareAllFriends");
        var i = 0, l = userCount, userID;
        for (; i < l; i += 1) {
            userID = userList[i].id;
            compareFriendsWith (i, userID);
        }
    }
    function compareFriendsWith (i, id) {
        //console.log("compareFriendsWith", i, id);
        var path = "/me/mutualfriends/",
            url = path + id + "/";
    
        FB.api(url, (function (i) {
            return function (response) {
                //console.log(i, response);
                var numFriends = (response.data) ? response.data.length : 0;
                setMutualFriends(i, numFriends);
                userCount -= 1;
                //console.log(userCount);
                if (userCount === 0) {
                    display_results();
                    //console.log("DISPLAYING");
                }
            };
        }(i)));
    }
    
    function setMutualFriends (i, friendcount) { userList[i].mfCount = friendcount; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Array if a user's Facebook friend list as follows: [ {
using PHP and MySQL i have grabbed an array of facebook user ids from
I have the following code: $facebook = new Facebook(array( 'appId' => ID, 'secret' =>
I have collections of users and posts. User looks like { _id : ObjectId(5089cc4c7b03b9902b000000),
I have an app which allows users to log in via Facebook, and all
I have used the Facebook PHP SDK to fetch the pages user likes. $liked
I have an application where users can register themselves using Facebook credentials. Now consider
I have a-facebook-like mentioning feature in my app. When creating posts or comments user
I am using the following code to store my facebook app users details in
I retrieve an array of friends using the graph api, and have a list

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.