0

由於臉譜圖只能返回100個用戶喜歡("/me/likes?limit=100")我做了一個循環,重複,直到所有喜歡加載。因此,我有一個單獨的圖形api結果對象,並且在循環完成後,我需要將它們合併爲一個。下面是一個有兩個Facebook的迴應如何合併兩個臉譜圖api數據結果(JSON)

var responses = [ 
    { 
    "data":[ 
     { 
     "category":"Community", 
     "name":"Uxcamp.pl", 
     "id":"1401334970104742", 
     "created_time":"2015-05-28T12:05:13+0000" 
     }, 
     { 
     "category":"Musician/Band", 
     "name":"The Shins", 
     "id":"129599657069433", 
     "created_time":"2015-05-21T15:59:10+0000" 
     } 
    ], 
    "paging":{ 
     "cursors":{ 
     "before":"MTQwMTMzNDk3MDEwNDc0Mg==", 
     "after":"MTI5NTk5NjU3MDY5NDMz" 
     }, 
     "next":"https://graph.facebook.com/v2.3/999625400069239/likes?access_token=CAAKvGzm…i7RfCs4rI5yqCsUxKJA87vpgMiYo8IYku59uHt&limit=2&after=MTI5NTk5NjU3MDY5NDMz" 
    } 
    }, 
    { 
    "data":[ 
     { 
     "category":"Other category", 
     "name":"Other event", 
     "id":"2342340104742", 
     "created_time":"2015-02-28T12:05:13+0000" 
     }, 
     { 
     "category":"Musician/Band", 
     "name":"Rammstein", 
     "id":"129543547069433", 
     "created_time":"2015-03-21T15:59:10+0000" 
     } 
    ], 
    "paging":{ 
     "cursors":{ 
     "before":"MTQwMTMzNDk3MDEwNDc0Mg==", 
     "after":"MTI5NTk5NjU3MDY5NDMz" 
     }, 
     "next":"https://graph.facebook.com/v2.3/34095843095874309438/likes?access_token=CAAKvGzm…i7RfCsIOSIOS(o8IYku59uHt&limit=2&after=MTI5NTk5NjU3MDY5NDMz" 
    } 
    } 
]; 

// function... 
// response = mergeAll(response); 

回答

1

短陣嘗試

var likes = []; 

responses.forEach(function(response, index, array) { 
    likes = likes.concat(response.data); 
}); 

console.log(JSON.stringify(likes)); 

這應該給你

[ 
    { 
     "category": "Community", 
     "name": "Uxcamp.pl", 
     "id": "1401334970104742", 
     "created_time": "2015-05-28T12:05:13+0000" 
    }, 
    { 
     "category": "Musician/Band", 
     "name": "The Shins", 
     "id": "129599657069433", 
     "created_time": "2015-05-21T15:59:10+0000" 
    }, 
    { 
     "category": "Other category", 
     "name": "Other event", 
     "id": "2342340104742", 
     "created_time": "2015-02-28T12:05:13+0000" 
    }, 
    { 
     "category": "Musician/Band", 
     "name": "Rammstein", 
     "id": "129543547069433", 
     "created_time": "2015-03-21T15:59:10+0000" 
    } 
] 

的結果。

http://jsfiddle.net/4wqw23qc/

+0

是啊,只有串接數據項是什麼,我需要在這種情況下,即使我沒有合併整個對象,所以答案是99%正確,謝謝:) –

+0

我不明白你的意思。如果有幫助,請接受我的回答。 – Tobi

+0

提出的解決方案合併了所有response.data項目,而不是響應,其中還包含「分頁」信息,但在這種情況下,分頁顯然是不必要的。 –