如何合并两个 facebook 图 api 数据结果 (JSON)

How to merge two facebook graph api Data results (JSON)

因为 facebook 图只能 return 100 个用户点赞 ("/me/likes?limit=100") 我做了一个循环,重复直到所有点赞都被加载。结果我有一个单独的图形 api 结果对象,循环完成后我需要将它们合并为一个。下面是一个包含两个 facebook 响应的短数组

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);

尝试

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/