Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda