0

I have a search bar, a drowdown and a filtered list which currently works as it should, but what I want to do is replace 'name' in filterV.product.name with a value selected from a dropdown.

 const filterArray = ["name", "description", "condition", "purpose", "category"];

one of these values should replace name when the user selects it from the dropdown and searches with the filter on.

 {this.state.filteredContent.length > 0 ? this.state.filteredContent.filter((filterV) => {
                        return   filterV.product.name.toLowerCase().indexOf(this.state.searchValue.toLowerCase()) !== -1
                    }).map((item, index) => (
                        <div key={index}>
                            <ProductItem
                                goToLink={true}
                                delete={false}
                                edit={false}
                                remove={false}
                                duplicate={false}
                                item={item}
                                hideMore
                            />
                        </div>
                    )): null}

any help much appriciated.

also even though the filter function is working and displaying the list on screen correctly, when I console.log this.state.filteredContent the length never changes, any ideas how I can get the correct list of filtered objects?

the list comes throught redux props

 this.state = {
        searchValue: '',
        filterValue: '',
        filteredContent: props.productWithoutParentList.length > 0 ? props.productWithoutParentList : [],
    }
4

1 Answer 1

1

You can update the filter using [] dynamic property access notation:

{this.state.filteredContent
        .filter((filterV) => {
          return (
            filterV.product[this.state.filterValue]
              .toLowerCase()
              .indexOf(this.state.searchValue.toLowerCase()) !== -1
          );
        })

Note that I've also set a default value of filterValue: "name"

Edit condescending-liskov-wiqxn

Sign up to request clarification or add additional context in comments.

4 Comments

Any idea why once filtered the length of the state.filteredContent remains the same?
@Zinox you're never really updating state.filteredContent, Array.filter() returns a new filtered array, you're using that one for rendering. It gets created and thrown away during the rendering process
Do remember that if you decide to update it, you'll need a copy of original so that when filters reset, you can put everything back...
Ofcouse. thank you for the reminder. :) I keep forgetting filter returns a new array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.