I have the following code:
const filterTable = (tableData, search) => {
return tableData.filter(
(data) =>
!search || data.name.toLowerCase().includes(search.toLowerCase()),
)
}
And I want to make the "name" prop be something dynamic based on an filterBy param, something like that:
const filterTable = (tableData, search, filterBy) => {
return tableData.filter(
(data) =>
!search || data.filterBy.toLowerCase().includes(search.toLowerCase()),
)
}
Example 1: considering that filterBy can be "description" instead "name" the final result is:
const filterTable = (tableData, search, filterBy) => {
return tableData.filter(
(data) =>
!search || data.description.toLowerCase().includes(search.toLowerCase()),
)
}
Example 2: considering that filterBy can be "state.city" instead "description" the final result is:
const filterTable = (tableData, search, filterBy) => {
return tableData.filter(
(data) =>
!search || data.state.city.toLowerCase().includes(search.toLowerCase()),
)
}
Example 2 wont work when I pass nested object "state.city".
There's any way to do it? Glad for any help!