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

202
Visualizações
Filter response from strapi

I'm trying to make a filter based on a Strapi relation.

My Strapi response looks like this: enter image description here

I'm using Next Js for the frontend, so I assign props like so:

return {
    props: {
        projects: data.projects.data,
        categories: categoriesData.data.categories.data,
    },
}

I map through all of the project and list them all as cards. Then I want to filter them based on a button click. The buttons are the categories names and I map over that array as well. On click of each button I run a function called "handleProjects" and this is where I run into an issue.

I have previously made a filter using SanityCMS and Next Js, however, the code structure of the response in Sanity is much clearer compared to that of Strapi.

This is the code I've used in my previous project with Sanity:

const [filteredProducts, setFilteredProducts] = useState(products)

function handleProducts(e) {
    let categoryType = e.target.value
    setFilteredProducts(
        products.filter((product) => product.category == categoryType)
    )
}

I'm using this query where I alias the category as such and it is much simpler:

  const productQuery = `*[_type == "product"] {
    _id,
    slug,
    name, 
    image, 
    "category": category->name
    }`

My function for my Strapi project looks like this:

function handleProjects(e) {
    let categoryType = e.target.value
    setFilteredProjects(
        projects.filter((project) =>
            project.attributes.categories.data.map((i) =>
                i.attributes.title.includes(categoryType)
            )
        )
    )
}

I'm having an array of objects, but at some point I have to map over an array of objects one level deeper and see if the categoryType matches some of the object values two levels deep.

Can anyone show me what I'm doing wrong here?

about 4 years ago · Juan Pablo Isaza
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