0

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

5
  • 2
    What result are you trying to get, and what are you getting instead? Commented Apr 11, 2022 at 15:04
  • What is payload? Commented Apr 11, 2022 at 15:04
  • The JS has mismatched brackets. Commented Apr 11, 2022 at 15:06
  • When I pass in the profile_id I would like to only get back the objects with that profile_id in the principal_investigator. I am currently getting back the the entire reporterData object-- over 500 records payload is the profile_id variable passed in from a different function. I should have cleaned that up Commented Apr 11, 2022 at 15:09
  • Then you should be filtering the reporterData array, not mapping it, if you don't want to get back all the elements. Commented Apr 11, 2022 at 15:27

2 Answers 2

3

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)
})
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry- but I don't get any results returned
Check edited answer.
I don't think your edits should make any difference.
Instead of filter() and length, you can use the some() method. But it looks like this should work.
|
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);
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.