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

138
Views
Filtra múltiples propiedades y valores en js/react

Quiero poder filtrar por estado y prioridad, pero también poder tener múltiples tipos de prioridad o estado, por ejemplo:

 [ { "id": 1, "status": "IN_PROGRESS", "priority": "NONE", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, { "id": 2, "status": "WAITING", "priority": "HIGH", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, { "id": 3, "status": "COMPLETED", "priority": "NONE", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, ]

Actualmente estoy usando .filter para filtrar con el valor que elijo, pero cada vez que elijo más de un valor para la misma propiedad ("ESPERANDO" y "EN PROGRESO" para el estado) obtengo una matriz vacía como retorno.

Aquí está mi código:

 allRequests.filter((item) => { if (filters !== {}) { for (const key in filters) { if (item[key] === undefined || item[key] != filters[key]) { return null; } } } return item; })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Javascript array.filter espera una función de predicado y devuelve un conjunto filtrado de elementos de matriz en función de esa función de predicado.

En lugar de devolver un elemento, debe devolver un valor booleano de true o false . Esta podría ser la razón por la que se enfrenta a este problema.

Para filtrar por múltiples estados y prioridades, cree un objeto de filtros como el siguiente:

 const filters = { "status": ["COMPLETED", "IN_PROGRESS"], "priority": ["NONE"] };

luego actualice su código a:

 allRequests.filter((item) => { if (Object.keys(filters).length > 0) { for (let key in filters) { if (item[key] === undefined || !filters[key].includes(item[key])) { return false; } } } return true; })

ver :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://zetcode.com/javascript/array-filter/

Puede encontrar un ejemplo de trabajo a continuación:

 const allRequests = [ { "id": 1, "status": "IN_PROGRESS", "priority": "NONE", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, { "id": 2, "status": "WAITING", "priority": "HIGH", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, { "id": 3, "status": "COMPLETED", "priority": "NONE", "inspectorId": 2, "reportAt": "2021-08-09T12:06:03+00:00", "scheduledFor": "2021-08-12T12:06:03+00:00", }, ]; const filters = { "status": ["COMPLETED", "IN_PROGRESS"], "priority": ["NONE"] }; const result = allRequests.filter((item) => { if (Object.keys(filters).length > 0) { for (let key in filters) { if (item[key] === undefined || !filters[key].includes(item[key])) { return false; } } } return true; }); console.log(result);

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!