.filter()Concept: Refactoring search filtering using array helpers
In Kata 8 we filtered names with a manual for loop. Letβs modernize that code by switching to .filter() and keeping the search logic tidy.
Create a SearchNamesArrayMethod component that:
['Alice', 'Bob', 'Charlie', 'David', 'Eve'].filter() to show only the names containing the search text (case-insensitive)Typing updates the search input and immediately filters the list, just like Kata 8 β only the implementation changes.
import { useState } from 'react';
export default function SearchNamesArrayMethod() {
const names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve'];
const [search, setSearch] = useState('');
// Use .filter() here
return (
<div>
{/* Add search input */}
{/* Display filtered results */}
</div>
);
}
.includes() works great inside the .filter() callback''), so you still get all names when the search is blankResults: {filteredNames.join(', ')}
.filter() keeps the code short and expressive when selecting items.filter() with string helpers like .toLowerCase() and .includes()