Tips for combining two sets of data from Facebook Graph API (in JSON format)

In order to overcome the limitation of Facebook graph API only returning 100 user likes ("/me/likes?limit=100"), I devised a loop that continues until all likes are retrieved. This results in separate graph API response objects, which need to be merged into one after the loop completes. Below is a brief example array showcasing two Facebook responses:

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

Answer №1

Give it a shot

let favorites = [];

responses.forEach(item => {
    favorites = favorites.concat(item.data);
});

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

This will generate

[
    {
        "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"
    }
]

as the outcome.

Take a look at

http://jsfiddle.net/4wqw23qc/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"An error has occurred: the data from axios is

Struggling with making an axios get request. axios .get("http://10.10.0.145/api/session", { headers: { 'Cookie' : user_cookie } }, ) .then(res => { result = res.data; id_user_in ...

Puppeteer encounters difficulty when attempting to click on a particular div element within Gmail

I am attempting to click on the three dots in Gmail by following this link: 1 After that, I want to click on the 'mark all as read' option: 2 Clicking on the three dots works without any issues. However, I am encountering difficulty when tryin ...

I am looking to develop a unique event that can be triggered by any component and listened to by any other component within my Angular 7 application

Looking to create a unique event that can be triggered from any component and listened to by any other component within my Angular 7 app. Imagine having one component with a button that, when clicked, triggers the custom event along with some data. Then, ...

JavaScript function produces erratic outcomes due to variances in input data

Experimenting with JavaScript today led me to discover something strange when using the spread syntax and fill() function. Here is the code I used: let v = [...Array(9).fill([])]; function myFunc(){ arr = [1,5,2,3,4,6,8,9,7]; ...

JavaScript progress bar with extended duration, inspired by the success-oriented approach

Currently, I am in search of a unique solution or plugin that can replicate the same effect as Kickstarter's crowd fund long term progression bar. It seems like existing solutions only offer short term dynamic progress bars that indicate the percentag ...

transmitting blob information within a JSON document for retrieval by an Android application

I am facing an issue while trying to include an image in a JSON file that is sent to an Android app. Below is the code snippet: public function allUserCarsAction() { // Code for fetching user cars } Below is the entity configuration for ...

Creating an installation package for an Electron application on Windows

Is it possible to create a Mac installer for an Electron app using a Windows PC? I have tried running npm make, but it only generates a Windows installer. ...

How to Handle Nested JSON Objects in Node.js

Struggling to send a nested JSON object to my node.js server. This is the expected structure of the JSON object: { "name" : "Plan 1", "schedule": [ { "start": 0, "days": [ { "t ...

Using ASP.NET MVC 6 Web API along with Angular, a resource can be posted as an object property in the

I am facing a challenge in sending a complex object from Angular to my web API. Here is how the object looks: { Name: "test", Tax: 23, Addresses: [ { country: "ro", city: "bucharest" }, { country: "fr", ...

Deciphering the Sequence of Definitions in TypeScript and Angular JS

As a newcomer to Angular JS and TypeScript, I have encountered an issue while going through the hero tutorial. The problem arises when defining the Hero class between @Component and AppComponent instead of at the beginning or end of the file. This causes ...

How can the required flag be integrated with rules validation in react-hook-form and material-ui (mui) for inputs?

Currently, I have implemented react-hook-forms for handling form functionality and validation in our application. On the other hand, we are utilizing MUI/Material-UI as our component library. One issue that arises is that MUI automatically adds a * to inpu ...

Restricting Options in jQuery/ajax选择列表

Although there are similar questions posted, my specific issue is that I am unsure of where to place certain information. My goal is to restrict the number of items fetched from a list within the script provided below. The actual script functions correct ...

Determine whether the click occurs inside or outside of a bar on a ChartJS graph

I'm currently working with a bar graph using chartJS. I'm trying to figure out how to detect where the user clicked - whether it was inside or outside of the bar region in chartJS. const waterFChart = new Chart(canvasRef.current, { plugins: [ ...

Enhancing WooCommerce by including additional text following the price for specific shipping methods

I need assistance with including a short message like "(incl. VAT)", following the shipping-price display on the checkout page. The challenge lies in ensuring that this message only appears for a specific shipping method within Zone 1 (zone_id=1), but I&a ...

How can I use Java script to find specific text within table rows on a website?

I am looking to create a dynamic table that filters rows based on user input in an input text box. Here is an example table: Row 1: Apples Row 2: Oranges Row 3: Bananas When a user starts typing in the text box, I want the rows to filter accordingly. ...

AngularJS/Restangular index-based extraction of JSON data

I have developed an application that takes input for quotes (purity, weight, total) and stores it in the $scope.quote array: // Controller action // $scope.quote.push({ total: ((($scope.karat * $scope.spot) * $scope.percentage) / 20) * $scope.estimated ...

Properly showcasing commas

Need help adding commas to numbers. Currently, my output is 1,2,3,4,5,6,7,890 but I want it to be 1,234,567,890. I am using keyup which is causing some problems. Any suggestions? numberWithCommas : function () { var target = $("#foo"); target.val(targ ...

How can I display both numbers and percentages on a pie chart using ChartJS datalabels?

I am currently using ChartJS in my React application to display data on a pie chart. My requirement is to display both the number and percentage values on each slice of the chart (e.g. 5(6.7%)). However, with the existing code, I am only able to show the ...

A guide to implementing offline.js or online.js alongside a submit button

I am looking for a way to check network connection only when the user presses the SUBMIT button, without constantly monitoring for internet connectivity. After researching websites and Stack Overflow questions for weeks, I have not found a satisfactory sol ...

The React.js .map function encountered an error while trying to map the results from Firebase

As a newcomer to the realm of React and Firebase, I find myself struggling with arrays and objects. It seems like the way my data is formatted or typed does not play well with the .map method. Despite scouring Stack Overflow for answers, none of the soluti ...