簡體   English   中英

使函數返回帶有對象的數組

[英]Make a function return an array with objects

我已經用這個在各個角落打了我的頭,但是我在這里錯過了一些東西。

信息

我正在編寫一個小的javascript頁面,該頁面將帶有Facebook照片的照片ID,然后返回總人數和共享該照片的人,然后在每張照片上找到喜歡的人數,並將其附加到該照片上。

基本上是“計算喜歡的人”

到目前為止,我設法查詢了股份總數,並找到了喜歡的股份,但是沒有安排的感覺,因為獲得股份的FB.API函數與獲得股份的FB.API函數是一個不同的請求。喜歡。

目標

我一直想做的就是獲取共享的圖像ID(用於獲得喜歡總數),人物名稱,人物ID並將它們作為對象推送到數組中。

然后,我將循環處理數組並附加喜歡的數目,同時檢查並確保喜歡屬於正確的人員ID。

所以我的最終數據數組如下所示:

[{"id" : "pageID_PostID", "name" : "Some Name", "idmin" : "This is the person ID", "likes" : "This is the total number of likes"}, {etc}, {etc}]

問題

1-console.log-ing數組我將對象推入設置該函數的函數的OUTSIDE,盡管我全局定義了該數組,但它返回為空數組。 在console.log-loging到函數內部時,它會按預期返回。

2-之所以不能將getLikes函數與find share函數一起使用,是因為它以未定義或不匹配的形式出現,我認為是因為它是另一個FB.api調用,並且需要花費幾毫秒的時間來查詢和獲取結果, JS決定其未定義的位置。

3-因為目標是將所有格式都以HTML格式放置,所以我需要以一種不錯的格式來排列結果,以便我使用它而不會造成麻煩。 哪個是數組[對象,對象,對象]

var pageAccessToken;
var shares = new Array();

 function getShares(id){

 FB.api('https://graph.facebook.com/' + id +'/sharedposts?access_token='+pageAccessToken, function(response)
      {
        for(var i = 0; i < response.data.length; i++)
        {
          if(response.data[i].story.indexOf('shared')){
              var shareID = response.data[i].id;
              var pageID = response.data[i].from.id;
              var shareObj = new Object();

              console.log(response.data[i].story);
              console.log(" With Post ID " + shareID);

              shareObj.id = response.data[i].id;
              shareObj.name = response.data[i].from.name;
              shareObj.idmin = response.data[i].from.id;
              shareObj.likes = null;

              shares.push(shareObj);
          } //end if
        } //end for loop

        console.log(response.data.length + " People shared this photo.");
        console.log("Inside call for shares array" + JSON.stringify(shares));
        return shares; //I don't really get how return works cause its not helping anywhere!


       }); //end fb api

 } //end getShares

//function to retrieve likes
    function getLikes(pageID, idmin){
      FB.api('https://graph.facebook.com/' + pageID + '/likes?     summary=1&access_token='+pageAccessToken, function(response)
      {

        for (var i = 0; i < shares.length - 1; i++) {
              shares[i].likes = response.summary['total_count'];
              console.log(shares[i].name + " has " + shares[i].likes + " likes.");
              console.log(response.summary['total_count']);

        }


  });
}
//end getLikes


  for (var i = 0; i < shares.length - 1; i++) {
          getLikes(shares[i].id, shares[i].idmin);
  }

  getShares(insert a photo ID here);
  console.log("2 Outside call for shares array" + JSON.stringify(shares));

由於FB.api函數是異步的並且需要一些時間來響應,因此您也應該對函數采用異步方法。 而不是返回的shares變量,您可能會收到一個功能參數在getShared功能,並調用它傳遞shares作為參數,當你准備好了。 這是一個簡單的示例:

function getShares(id, callback) {
    FB.api('https://graph.facebook.com/' + id +'/sharedposts?access_token='+pageAccessToken, function(response) {
        // ...do something with the response and store the result in a variable called shares...

        callback(shares); // calls the function passed as argument passing "shares" as parameter
    }
}

然后,您可以調用getShares並將函數作為參數傳遞給您要對結果執行的操作:

getShares(some_id, function(shares) {
    alert('shares: ' + JSON.stringify(shares));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM