I built a search bar that will filter an array of objects. But I feel like I can improve it. When I filter, I want to filter the first and last name case insensitive, then sort the names and then map them to be rendered.
I'm using typescript so ignore SelectedProps:
return contacts
.filter((item: SelectedProps) => (item.firstName + item.lastName).toLowerCase().includes(search.toLowerCase()))
.sort((a: SelectedProps, b: SelectedProps) => a.firstName > b.firstName ? 1 : -1)
.map((item: SelectedProps, index: number) => (
// IGNORE OUTCOME
));
localeComparefor your sort function, I probably wouldn't change anything here. What are you looking for in answers? \$\endgroup\$(item.firstName + item.lastName).toLowerCase()into named utility functions likeloweredName(item)andsortByFirstName, and use those within this code. \$\endgroup\$