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

90
Visualizações
Filter an Array of Objects inside an Array of Objects

I have a mern app and get all albums via axios.

the structure is like :

[
 {
  title: "",
  artist: "",
  reviews: [
   {
     username: "",
     comment: "",
   },
   {
     username: "",
     comment: "",
   },
  ]
 },
 {
  title: "",
  artist: "",
  reviews: []
 },
]

I need to filter every review.object that has a specific username inside for example BUT return the object which has that review array inside.

if index[3] res.data -> reviews -> object has username return that res.data object. I tried with filter inside filter but it did not work.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create a function that gets the username, then map and filter it so you can display only the datas you need

Try this

const datas = [
    {
        title: "",
        artist: "",
        reviews: [
            {
                username: "a",
                comment: "",
            },
            {
                username: "b",
                comment: "",
            },
        ]
    },
    {
        title: "",
        artist: "",
        reviews: []
    },
]

const filteredReviews = username => {
    return datas.map(data => {
        if (data.reviews.length > 0) {
            if (data.reviews.filter(review => review.username == username).length > 0) {
                return data
            }
        }
        return null
    }).filter(data => data)
}

by this example, I returned null inside the map for those object that I don't need, then filter the array for it's element that has a value

Then you can get the object you need by calling that function like this

console.log(filteredReviews('b'))
about 4 years ago · Juan Pablo Isaza 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