• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

63
Vistas
How to fiter array with an array

I have a list of objects like this.

results = [
   { id: 1, 
     status : "Active"
     // Some other fields
     tags : [{val: 'IGM', color: 'light-success' },
             {val: 'Gated Out', color: 'light-primary' },
            ]   
   },
   // ...
]

now I want to filter objects in relation to tags, the input to filter the list is also in the form of an array using multi-select input.

like

[{value: 'Gated Out', label: 'GATED OUT'},  .. ]

I'm able to filter data of other fields but not the tags because other fields are in strings and tags are an Array.

But now How can I modify this to work with the array as well.

I'm using that approach;

  const handleTagsFilter = (value) => {
    let updatedData = []

    const dataToFilter = () => {
      if (
        status.length ||
        custom_tags.length
      ) {
        return filteredData
      } else {
        return results
      }
    }

    setCustomTags(value)
    if (value.length) {
      updatedData = dataToFilter().filter((item) => {
        const startsWith = item.status.toLowerCase().startsWith(value.toLowerCase())

        const includes = item.status.toLowerCase().includes(value.toLowerCase())

        if (startsWith) {
          return startsWith
        } else if (!startsWith && includes) {
          return includes
        } else return null
      })
      setFilteredData([...updatedData])
      setCustomTags(value)
    }
  }

That function works with filtering strings like we have the status field to Active than this work, But I'm not sure how I can modify it to work with the array as well.

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Maybe something like:

let search_str = 'abc'.toLowerCase();
let filtered_results = results
  .map(v => v.tags.filter(_v => _v.val.toLowerCase().includes(search_str)))
  .filter(v => v.length)
  .reduce((a, b) => a.concat(...b), [])
about 3 years ago · Juan Pablo Isaza Denunciar
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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda