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

244
Views
Filter an nested Object with filter and map with JavaScript

I know that this is close to a duplicate but I can't get the code to work. I have an object that I need to filter and I'm currently trying to emulate the accepted as an answer the code at Javascript filtering nested arrays

My data object is:

[{
    "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"
    }]
  }
]

and my JavaScript code is:

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

I need to pass in a profile_id as a payload and return the objects that will fill a data table.

The data can be 1000's of items and the principla_investigators can be multiple entries. When I use the code that I have it return all of the objects. Can someone point out my error? Thanks

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can try doing like this:

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

I understand that you want an array with all the investigators matching that ID, right?

Try this:

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
}, [])

You can also do for loops with the same result:

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!