Multiple Filter Combination
What you'll learn: How to construct API queries that apply multiple filters simultaneously and understand how they logically combine.
When building real-world applications, you often need to filter data by more than one criterion at once. For example, searching for products that are both "in stock" AND under $50, or posts that match "JavaScript" OR "TypeScript" in their title.
How Filters Combine
Most REST APIs use implicit AND logic when combining multiple query parameters. When you pass ?category=shoes&color=red&inStock=true, the server typically returns only items that match ALL three conditions.
// Fetch products matching ALL filters
fetch('/api/products?category=shoes&color=red&inStock=true')
.then(res => res.json())
.then(data => console.log(data));
This translates to: "Give me products WHERE category = shoes AND color = red AND inStock = true"
Understanding the Query String
Each filter adds a condition:
- Single filter:
?status=active→ "status equals active" - Two filters:
?status=active&role=admin→ "status equals active AND role equals admin" - Three filters:
?status=active&role=admin&country=US→ all three must match
Some APIs support OR logic through special syntax like ?tags=javascript,typescript (comma-separated) or repeated parameters ?tag=javascript&tag=typescript, but this varies by API design.
Building Dynamic Filter Queries
You can programmatically construct complex filter combinations:
const filters = { category: 'shoes', color: 'red', inStock: true };
const params = new URLSearchParams(filters);
fetch(`/api/products?${params.toString()}`)
.then(res => res.json());
Key Takeaway: Multiple filters in query strings typically combine with AND logic, requiring all conditions to be met. Build complex queries by chaining parameters, and always check the API documentation for specific combination rules.