Welcome to Lesson 5 of Communicating with APIs for Front-End Engineers. Today's focus is on handling data fetched from APIs, transforming JSON data into JavaScript objects, and manipulating data using the filter, map, and reduce methods. We'll also discuss how to handle errors that may occur during the transformation process.
Data transformation refers to the process of converting data from one format or structure into another. When handling APIs, we're dealing primarily with data fetched in the JSON (JavaScript Object Notation) format, a lightweight, human-readable data interchange format. For efficient processing, we convert the JSON data into JavaScript objects.
The JSON.parse() method allows us to convert JSON data into JavaScript objects:
In this example, obj is a JavaScript object that contains the properties name and age derived from the JSON string.
JavaScript provides powerful methods for array manipulation — filter(), map(), and reduce().
The filter() method returns a new array containing elements that satisfy the given condition. For instance:
The map() method returns a new array, with each element transformed by a given function. For example:
The reduce() method applies a function across elements in the array, cumulatively computing a single result. For example:
These methods are incredibly beneficial for processing and manipulating fetched data.
