Kevin
Kevin

Reputation: 75

How to debug this search filter in react

I made a simple search filter and it is working properly in the sense that it will return back the number of results back with it being filtered.

here is a bit of the code to help understand what I mean

{this.state.subject.filter(search(this.state.search))
    .map((subject,index) => {
      return <Subject key={index} 
        subjectId={this.state.subject[index].id}
        subjectName={this.state.subject[index].name} />
})}

As you can see to access the subjectI need to access the array index inside in order to get the result. If i search for something and say returns 2 it will return the first two index instead of the specific ones.

Is there a way to get around it?

Upvotes: 0

Views: 76

Answers (1)

Andre Knob
Andre Knob

Reputation: 831

You don't need to access elements from the original array, you can use the category of the corresponding iteration inside your map call:

{this.state.categories.filter(searchingFor(this.state.search))
  .map((category) => {
    return <Category key={category.id} 
      categoryId={category.id}
      categoryName={category.name} />
})}

Upvotes: 1

Related Questions