0

I have an array like:

const property = [ {name: Demo}, {name: Test}]

const I also Have a state for search Value

const [searchText,setSearchText]  = useState('')

whenever the search Text Change I am firing a useEffect()

useEffect(() => {

//Do  Something

},[searchText])

What I wanna do is to filter out the value of the property array based on the searchText.

useEffect(() => {

const data = property.filter(el => el.name === searchText)
console.log(data).

},[searchText])

But by doing this way we have to write exact same name in the search box to filter out the value

like:

useEffect(() => {

const data = property.filter(el => el.name === searchText) // In here the el.name need to be exact the searchText value in order to filter the array
console.log(data).

},[searchText])

I want if someone types a letter like "b" then filter out all the result except which one has starts with "b". If anyone writes "The" then I wanna filter out all the value except which one has "The"

  • @pilchard yeah. This is what I wanna do. Can U tell me what should I do with my existing question. I mean should I remove it – Asaduzzaman Himel Mar 22 '21 at 16:26
  • You can leave it, it will redirect people to the other answer, but see: https://meta.stackoverflow.com/questions/355459/should-i-delete-duplicate-questions – pilchard Mar 22 '21 at 16:40

0 Answers0