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

89
Vistas
Filtered object search using allowed keys and also a search value

as seen in my title I am facing a problem that needs a solution. I am trying to filter my object, but not only based on keys. But also based on the value of the allowed keys.

This is what I got so far:

    const handleSearch = (searchValue) => {
        if(searchValue !== '') {
            const allowed = ['name', 'title'];
            let list = props.search.list;

            const filtered = Object.keys(list)
              .filter((key) => allowed.includes(key))
              .reduce((obj, key) => {
                obj[key] = list[key];
                return obj;
              }, {});
            

            const filteredArray = Object.entries(filtered)


            props.search.onChange(filteredArray)
        } else {
            props.search.onChange(props.search.list)
        }
    }

Structure of the list object:

0: {name: "John', title: 'Owner'}
1: {name: "Jane", title: 'Admin'}

Wanted results:

Filtered array that shows the keys filtered aswell as the search value.

And I don't know where I should integrate the filtering by the values aswell. This has been giving me a headache for the past few hours. And hope someone here is experienced with these kinds of issues/logic.

Thanks for reading.

Kind regards.

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

0

const handleSearch = (searchValue) => {
    if (searchValue !== '') {
        const allowed = ['name', 'title'];
        const list = props.search.list;

        const filtered = list
            .filter(obj =>
                Object.keys(obj)
                    .some(k => allowed.includes(k))
            )
            .filter(obj =>
                Object.values(obj)
                    .map(v => v.toLocaleLowerCase())
                    .some(v => v.includes(searchValue.toLowerCase()))
            )

        props.search.onChange(filtered)
    } else {
        props.search.onChange(props.search.list)
    }
}

Example

Let's assume props as:

const props = {
    search: {
        list: [
            { name: "John", title: 'Owner' },
            { name: "Jane", title: 'Admin' },
            { name: "Reza", title: 'Owner' }
        ],
        onChange: x => console.log(x)
    },
}
handleSearch("own")

// [ { name: 'John', title: 'Owner' }, { name: 'Reza', title: 'Owner' } ]


handleSearch("jane")

// [ { name: 'Jane', title: 'Admin' } ]


handleSearch("something")

// []
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda