Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda