Conversation
| @@ -16,7 +16,16 @@ function renderProductsGrid() { | |||
| // filter the products that match the search. | |||
| if (search) { | |||
There was a problem hiding this comment.
I did it like this:
if (search) {
search = search.toLowerCase()
filteredProducts = products.filter(product => {
return product.name.toLowerCase().includes(search) ||
product.keywords.includes(search);
})
}There was a problem hiding this comment.
I did the same thing and it worked. U are smart, that's nice.
There was a problem hiding this comment.
is mine efficient like his ? :
if(key){
const searchTerm = key.toLowerCase();
filteredProducts = products.filter((productData)=>{
const searchData = (productData.name + productData.keywords.join(' ')).toLowerCase();
console.log(searchData)
return searchData.toLowerCase().includes(searchTerm)
})
}There was a problem hiding this comment.
@antaugustol
Your code would be correct if "const search" is changed to "let search"...Otherwise, it will throw an error saying constant variable can't be changed.
@SuperSimpleDev 's code throws it's own sets of errors. In the best case that it works, it is case sensitive meaning if you searched for "cover" it will show results but if you search "Cover", there will be zero matches...
There was a problem hiding this comment.
I did it like this:
if (search) {
search = search.toLowerCase()
filteredProducts = products.filter(product => {
return product.name.toLowerCase().includes(search) ||
product.keywords.includes(search);
})
}
it says you can't redeclare search so do this instead:
if (search) {
const searchLower = search.toLowerCase()
filteredProducts = products.filter((product) => {
return product.name.toLowerCase().includes(searchLower) ||
product.keywords.includes(searchLower)
})
}There was a problem hiding this comment.
Your second version is definitely the better approach. Creating a separate searchLower variable keeps things clear and avoids modifying the original search value. It also prevents the redeclaration issue and makes the filter logic easier to maintain.
|
Great points from everyone. Normalizing the search term with a separate searchLower variable is definitely the cleanest and most reliable approach. It keeps the code predictable, avoids redeclaration issues, and handles case-insensitive matching properly. |
No description provided.