Skip to content

Conversation

@SuperSimpleDev
Copy link
Owner

No description provided.


// If a search exists in the URL parameters,
// filter the products that match the search.
if (search) {
Copy link

@antaugustol antaugustol Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it like this:

  if (search) {
    search = search.toLowerCase()
    filteredProducts = products.filter(product => {
      return product.name.toLowerCase().includes(search) ||
        product.keywords.includes(search);
    })
  }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the same thing and it worked. U are smart, that's nice.

Copy link

@maasch maasch Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
    }) 
  }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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...

Copy link

@ibrahimmoalim ibrahimmoalim Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antaugustol

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)
  })
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Femi007-gh
Copy link

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.
Nice to see different solutions converging toward the same idea—good work, everyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants