Skip to content
Advertisement

Comparison of Multiple Conditions Using Filters in React TypeScript

I’m going to make a new arrangement by comparing the current list data with the filter data

I made a new arrangement by comparing one condition using a filter, and all I want to do is compare many conditions, so please help me

index.tsx

const [listData, setListData] = useState<Array<List>>([]);
const [filterData, setFilterData] = useState<Array<List>>(null!);

const fetchData = (form: FetchData) => {
    const filteredData = listData.filter(({ user }) => user === form.user);
    setFilterData(filteredData); //Compare only current users
};


Advertisement

Answer

You can just add more conditions to the filter itself:

const filteredData = listData.filter((user) => user === form.user && condition2 && condition3);

In case you would like to do some processing before checking a condition, you can add that as follows:

const filteredData = listData.filter((user) => {
    // some processing
    return user === form.user && condition2 && condition3;
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement