JavaScript · API · Beginner
Working with Categories
Fetch and explore product categories, then use them to filter the product list.
Quick topic start and explanations before exercises (exercises below):
Practical patterns with categories
#Category endpoints reference
#Exercises:
List all categories
#Fetch `/api/categories/` and print the total number of categories (`count`) and the `name` of each one.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// fetch /api/categories/ and print count + names
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
console.log(data.count);
for (const cat of data.results) {
console.log(cat.name);
}
}
main();
Find a category by name
#Fetch all categories, then find the one named "Bedroom" using `.find()` on `data.results`. Print its `id`, `name`, and `slug`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// find the "Bedroom" category and print id, name, slug
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
const bedroom = data.results.find(c => c.name === 'Bedroom');
console.log(bedroom.id);
console.log(bedroom.name);
console.log(bedroom.slug);
}
main();
Products in a category
#Fetch all categories, pick the first one from `results`, then fetch its products using `category=<slug>`. Print the category name and how many products it has.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// 1. get categories (use data.results)
// 2. use first category slug to filter products
// 3. print category name and product count
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const catResponse = await fetch(`${BASE_URL}/api/categories/`);
const catData = await catResponse.json();
const first = catData.results[0];
const params = new URLSearchParams({ category: first.slug });
const prodResponse = await fetch(`${BASE_URL}/api/products/?${params}`);
const prodData = await prodResponse.json();
console.log(first.name);
console.log(prodData.count);
}
main();
Count products per category
#For each category in `data.results`, fetch the product count and print `"CategoryName: N products"`. Use `page_size=1` to avoid loading unnecessary data.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// for each category in data.results: fetch count and print "Name: N products"
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const catResponse = await fetch(`${BASE_URL}/api/categories/`);
const catData = await catResponse.json();
for (const cat of catData.results) {
const params = new URLSearchParams({ category: cat.slug, page_size: 1 });
const prodResponse = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await prodResponse.json();
console.log(`${cat.name}: ${data.count} products`);
}
}
main();
Build a category slug to name map
#Fetch all categories and build a plain object that maps each `slug` to its `name`. Print the map, then look up the name for slug `"bedroom"`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// build { slug: name } map from data.results
// print the map and look up 'bedroom'
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
const categoryMap = Object.fromEntries(data.results.map(c => [c.slug, c.name]));
console.log(categoryMap);
console.log(categoryMap['bedroom']);
}
main();