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

88
Views
Búsqueda de objetos filtrados usando claves permitidas y también un valor de búsqueda

como se ve en mi título, me enfrento a un problema que necesita una solución. Estoy tratando de filtrar mi objeto, pero no solo en función de las claves. Pero también en función del valor de las claves permitidas.

Esto es lo que tengo hasta ahora:

 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) } }

Estructura del objeto de lista:

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

Resultados buscados:

Matriz filtrada que muestra las claves filtradas así como el valor de búsqueda.

Y no sé dónde debo integrar el filtrado por los valores también. Esto me ha estado dando dolor de cabeza durante las últimas horas. Y espero que alguien aquí tenga experiencia con este tipo de problemas/lógica.

Gracias por leer.

Atentamente.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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) } }

Ejemplo

Supongamos props como:

 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 Report
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!