Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda