0

I can just filter by input text that comes from "searchTerm" state. but I want to include "selectedOption" dropdown input to the filter. how can I use multiple filters for an array?

btw I'm using react-select for dropdown menu.

const Positions = ({ positions }: dataProps) => {

const options: any = [
  { value: "Canada", label: "Canada" },
  { value: "Italy", label: "Italy" },
];

const [selectedOption, setSelectedOption] = useState(null);

  const [searchTerm, setSearchTerm] = useState("");

  const filteredPositions = useMemo(() => {
    if (!searchTerm) return positions;
    return positions.filter(({ id, ...other }: any) => {
      const lowerCasedSearchTerm: string = searchTerm.toLowerCase();

      return Object.values(other)
        .map((v: any) => v.toLowerCase())
        .some((v: any) => v.includes(lowerCasedSearchTerm));
    });
  }, [searchTerm, positions]);

  return (

             <input
                  type="search"
                  placeholder="Search"
                  onChange={(e) => setSearchTerm(e.target.value)}
                />
              </div>
            <div className={styles.positions__item}>
              <Select
                defaultValue={selectedOption}
                onChange={setSelectedOption}
                options={options}
              />
              <Select
                defaultValue={selectedOption}
                onChange={setSelectedOption}
                options={options}
              />

   {filteredPositions?.map((position: any, index: any) => (
            <div key={position.position}>
              <SinglePosition
                category={position.category}
                type={position.type}
                position={position.position}
              />
            </div>
          ))}

3
  • You can do it like this some((v: any) => v.includes(lowerCasedSearchTerm) || v.includes(selectedOption)) Commented Jul 9, 2022 at 16:41
  • thank you but didn't work :( I tried with selectedOption.value still same. Commented Jul 9, 2022 at 16:47
  • Can you provide some sample input and output. It will be easier that way. Commented Jul 9, 2022 at 16:49

2 Answers 2

1

Check this please.

const filteredPositions = useMemo(() => {
if (!(searchTerm || selectedOption)) return positions;
return positions.filter(({ id, ...other }: any) => {
  const lowerCasedSearchTerm: string = searchTerm.toLowerCase();

  return Object.values(other)
    .map((v: any) => v.toLowerCase())
    .some((v: any) => v.includes(lowerCasedSearchTerm) || v.includes(selectedOption));
});
 }, [searchTerm, positions, selectedOption]);
Sign up to request clarification or add additional context in comments.

5 Comments

thank you but didn't filter selectedoption :( just worked on search input.
Could you please, provide the code on a live editor like codesandbox, So we can debug. codesandbox.io/s/new
I put it on index.js and app.js files. data comes from sanity no problem with searchTerm but dropdown doesn't filter.
If you don't mind, let's have a meeting so, I can debug the code in your environment.
im not available right now. but thank you
0

Try this:

const filteredPositions = useMemo(() => {
    if (!searchTerm) return positions;
    return positions.filter(({ id, ...other }: any) => {
      const lowerCasedSearchTerm: string = searchTerm.toLowerCase();

      return Object.values(other)
        .map((v: any) => v.toLowerCase())
        .some((v: any) => v.includes(lowerCasedSearchTerm) || v.includes(selectedOption.value.toLowerCase()));
    });
  }, [searchTerm, positions]);

Since, we are converting everything into lowercase, we need to convert selected option as well.

Comments

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.