7. Faceted Search Counts
On a listing page, the sidebar shows filter facets with counts: "Nike (12)", "Adidas (8)". Shoppers can tick several brands (combined with OR), and filters in different facets are combined with AND.
The bug users reported: after ticking Nike, every other brand showed "(0)", so nobody could add a second brand. The correct behaviour, used by every major store, is that a facet's counts ignore that facet's own selection but respect all the others. Selected values must stay visible even with 0 results so the shopper can untick them. Some attributes have several values per product (a shoe available in sizes 40, 41 and 42).
Your task:
Write getFacetCounts(products, facetKeys, activeFilters) and return { total, facets }.
Rules:
- products is a list of { id, ...attributes }. An attribute value is a string or an array of strings. The helper valuesOf(product, key) always gives you an array.
- activeFilters is { [facetKey]: [selected values] }. A product matches a facet when any of its values is selected. A product matches the filters when it matches every facet that has selections.
- total is the number of products that match all active filters.
- For each key in facetKeys, count for every value the products that match all active filters except this facet's own, and have that value. A product with several values counts once for each value.
- Each facet is a list of { value, count, selected }, sorted by count (highest first), then value A to Z.
- Leave out values with count 0 unless they are selected.
Helpers you can use
These functions are already defined. Call them from your code; you don't need to copy them.
valuesOf(product, key) → string[]The values of an attribute as an array: [] when missing, [value] for a single value.
Show the helper code
/** Returns the values of `key` as an array: [] when missing, [value] for a single string. */
function valuesOf(product, key) {
const value = product[key];
if (value === undefined || value === null) return [];
return Array.isArray(value) ? value : [value];
}Examples
Example 1
- Input
products = [ { id: 1, brand: "Nike", color: "Black", size: ["40", "41"] }, { id: 2, brand: "Nike", color: "White", size: ["41", "42"] }, { id: 3, brand: "Adidas", color: "Black", size: ["42"] }, { id: 4, brand: "Puma", color: "Black", size: ["40"] }, { id: 5, brand: "Adidas", color: "White", size: ["40", "42"] } ]facetKeys = ["brand", "color"]
activeFilters = { brand: ["Nike"] }
- Output
{ total: 2, facets: { brand: [ { value: "Adidas", count: 2, selected: false }, { value: "Nike", count: 2, selected: true }, { value: "Puma", count: 1, selected: false } ], color: [ { value: "Black", count: 1, selected: false }, { value: "White", count: 1, selected: false } ] } }
- Explanation
- Brand counts ignore the brand filter, so Adidas and Puma keep their counts. Color counts only look at Nike shoes.
Solution
Write getFacetCounts. Tab indents (Esc then Tab leaves the editor), Ctrl/⌘ + Enter runs. Each run has a 3-second limit. Your code is saved in this browser.
Test Results
Click "Run Code" to test your solution.