Manipulating data is a core skill for any developer. In an API-driven environment, so much of the data you receive is formatted in a way that doesn’t directly match the way that your application or UI needs it. Each web service and third-party API is different. This is where the ability to sort, normalize, filter, and manipulate the shape of data comes in.

In this article, we’ll explore some common ways to work with data in Javascript. To follow along, you’ll need to be working with code in either Node.js or the Browser.

Retrieving data from an API

Before we start, you need some data. For the rest of the examples in the article, we’ll be using the data returned from a search of GitHub’s v3 REST API. We’ll use the search/repositories endpoint to make a query for repositories matching a search term (the q parameter, in this case set to bearer). We’re also going to limit the number of results to 10 per page, and only one page. This makes it more manageable for our examples.

Start by using Fetch to connect to the API, and wrap it in a function with some basic error handling. You can reuse the function later in each of our examples.

const apiCall = () => fetch('https://api.github.com/search/repositories?q=bearer&per_page=10').then(res => {
  if (res.ok) {
    return res.json()
  }
  throw new Error(res)
})
.catch(console.err)

#javascript #arrays #rest-api #data-mapping #tutorial #data-science

How Use Array Methods to Handle API Data
1.15 GEEK