Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Filter an Array of Objects based on a conditional property

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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!