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

243
Views
Filtrar un objeto anidado con filtro y mapa con JavaScript

Sé que esto está cerca de un duplicado, pero no puedo hacer que el código funcione. Tengo un objeto que necesito filtrar y actualmente estoy tratando de emular lo aceptado como respuesta al código en Javascript filtrando matrices anidadas

Mi objeto de datos es:

 [{ "project_num": "5R01DA012513-23", "principal_investigators": [{ "profile_id": 2076451, "full_name": "PK", "title": "" }] }, { "project_num": "5R01DK118529-03", "principal_investigators": [{ "profile_id": 8590844, "full_name": "HW", "title": "PROFESSOR, SCIENTIFIC DIRECTOR" }] }, { "project_num": "3R01AA025365-05S1", "principal_investigators": [{ "profile_id": 8730036, "full_name": "JJ", "title": "ASSOCIATE PROFESSOR OF PSYCHIATRY" }] }, { "project_num": "1R01HL163963-01", "principal_investigators": [{ "profile_id": 2084037, "full_name": "KH", "title": "ASSOCIATE PROFESSOR" }, { "profile_id": 11309656, "full_name": "AM", "title": "RESEARCH ASSISTANT PROFESSOR" } ] }, { "project_num": "5R25HL092611-15", "principal_investigators": [{ "profile_id": 1886512, "full_name": "CW", "title": "P" }] } ]

y mi código JavaScript es:

 let payLoad = 1886512 const result = this.reporterData.map(t => { const principal_investigators = t.principal_investigators.filter(d => d.profile_id === payLoad); return { ...t, principal_investigators }; })

Necesito pasar un profile_id como carga útil y devolver los objetos que llenarán una tabla de datos.

Los datos pueden ser miles de elementos y los investigadores principales pueden ser entradas múltiples. Cuando uso el código que tengo, devuelve todos los objetos. ¿Alguien puede señalar mi error? Gracias

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puedes intentar hacer así:

 const result = this.reporterData.filter((t) => { const principal_investigators = t.principal_investigators.filter((d) => d.profile_id === payLoad) return (principal_investigators.length > 0) })
about 4 years ago · Santiago Trujillo Report

0

Entiendo que desea una matriz con todos los investigadores que coincidan con esa identificación, ¿verdad?

Prueba esto:

 const result = this.reporterData.reduce((previous, current) => { if (current.principal_investigators) { current.principal_investigators.forEach(pi => { if (pi.profile_id === payLoad) { previous.push(current) } }); } return previous }, [])

También puedes hacer bucles for con el mismo resultado:

 const result = []; for (project of this.reporterData) { if (project.principal_investigators) { for (pi of project.principal_investigators) { if (pi.profile_id == payLoad) { result.push(pi); } } } }
about 4 years ago · Santiago Trujillo 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!