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

78
Views
Matriz de filtro en objeto con condición múltiple

Estoy haciendo una función de búsqueda en la que el usuario ingresará la opción en el objeto de filtro, luego obtendrán la lista que desean. Así que tengo una matriz como esta

 let array = [ { "id": 1, "form_items": [ { "value_text": "test", "header_id": 1, "value_number" 2000 }, { "value_text": "test 2", "header_id": 2, "value_number" null } ] }, { "id": 2, "form_items": [ { "value_text": "test 3", "header_id": 3, "value_number": 1000 } ] } ]

y tengo un objeto para filtrar como este

 let filter = { "value_text": "test 2", "value_number": 2000 }

¿Cómo puedo devolver mi base de matriz en mi objeto de filtro? Ese elemento en la matriz form_items coincide con la condición, por ejemplo: "prueba 2" en "header_id": 2 y 2000 en "header_id": 1

Entonces mi resultado se verá así

 [ { "id": 1, "form_items": [ { "value_text": "test", "header_id": 1, "value_number" 2000 }, { "value_text": "test 2", "header_id": 2, "value_number" null } ] } ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede hacer esto con pocos bucles, pero podría incluirse en funciones más pequeñas.

 var results = []; for(const element of array){ // 1. Find all formItems that are matching your filter for each element let matchingFormItems = []; // create an result array of formItems matching your filter criteria for(const formItem of array['form_items']){ // iterate over all form_items for(let filterKey: Object.keys(filter)){ // for each filter key, check if current formItem matches filter criteria if(filter[filterKey] === formItem[filterKey]){ // if filter key is matching property from the formItem, push it to result Array matchingFormItems.push(formItem); break;// if one filter is matching, you can skip other } } } // 2. If at least one formItem has been found - push new object to the result array if(matchingFormItems.length > 0 ){ results.push({ // it's best not to mutate input array, so we need to clone the object with only desired formItems. 'id': element.id, 'form_items': matchingFormItems }); } }
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!