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

128
Visualizações
How do I use filter inside filter in react

I have an array getting from an API data, I would like to filter categories if it's in category. I was trying like :

data.filter((post) => post.categories.filter((category) => if(category._id === category) return category

It didn't work for me

Here is my array data :

export interface Post {
  categories: Category[]
  publishedAt: string
  _id: string
  _createdAt: string
  title: string
  author: {
    name: string
    image: string
  }
  comments: Comment[]
  description: string
  mainImage: {
    asset: {
      url: string
    }
  }
  slug: {
    current: string
  }
  body: [object]
}

export interface Category {
  _id: string
  _ref?: string
  title: string
}

and I tried this too

posts.map((post) => {
  post.categories.filter((category) => {
    if (category._ref === isCategory) return category;
  });
  {
    return <Posts post={post} />;
  }
});

How can I do it ?

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

0

I guess you want to get the posts having a specific category. You can filter the posts based on if some of the categories are equal to the defined category id.

Try like this.

{data
  .filter((post) => post.categories.some((cat) => cat._id === category))
  .map((post) => (
    <Posts post={post} />
  ))}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need at first get the list of post that have in the array category the dedicated category. Try this solution

const postsWithCategory = posts.filter((post) => post.categories.findIndex(cat => cat._ref === isCategory) !== -1);
postsWithCategory.map(post => (<Posts post={post} />));

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