In this activity, you will learn how to fetch user data from the JSONPlaceholder API, store it in a variable, and create modified objects for each user with specific fields using JavaScript.
- Write a JavaScript function (e.g.,
fetchUserData
) in the provided index.js file that performs the following tasks: - Fetch the data from
https://jsonplaceholder.typicode.com/users
usingasync/await
.
- Inside the
fetchUserData
function, store the fetched user data in a variable (e.g.,usersData
).
-
Still within the
fetchUserData
function, create modified user objects (maybe by using an array iterator method like.map
) for each user with specific fields as follows:- Name
- Username
- Address (Street name only)
- Phone
- Website
- Company (Company name only)
and put these new objects in an array.
-
Log the modified list.
- Filter the users data that you modified to display users only which have
.biz
domain in their email. - Log the filtered list.
- Sort the users alphabetically by name.
- Log the sorted list.
- Catch the errors and print out the error messages that might occur.
You can open up a terminal in your working directory and run the command node index.js
to run the program.
Alternatively, you can also use the nodemon
package to make your program run in real time whenever you save your file. This might need some googling, but it is not very hard to do.