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

102
Visualizações
Searchbar cant filter JSON File Javascript

today i have a problem with my searchbar.

const [posts, setPosts] = useState(null)
const [searchTerm, setSearchTerm] = useState("")

useEffect(() => {
    const loadPosts = async () => {
        try {
            const post = await getAllPosts()
            setPosts(post)
        } catch (e) {
            alert("Couldn't load posts")
        }
    }
    loadPosts()
}, [])

return (
    <div>
        <input type={"text"} placeholder="Search..." onChange={event => {
            setSearchTerm(event.target.value)
        }}/>

    </div>
)

}

This is my Searchbar Component. In the Index file, did i gave a props with.

const [posts, setPosts] = useState([])
const [searchTerm, setSearchTerm] = useState("")

useEffect(() => {
    const loadPosts = async () => {
        try {
            const post = await getAllPosts()
            setPosts(post)
        } catch (e) {
            alert("Couldn't load posts")
        }
    }
    loadPosts()
}, [])

return (
    <div className={styles.posts}>
        <h1>Market-place Valando</h1>
        <SearchList title={posts.filter(post => {
            if (post.title.toLowerCase().includes(searchTerm.trim().toLowerCase()) && searchTerm.trim() !== "") {
                return post.title
            }
        }).map(titles => {
                {
                    {titles.title}
                }
            }
        )}/>



        {
            posts.map(post => {
                return (
                    <div key={post.id} className={styles.key}>
                        <h1>{post.title}</h1>
                        <Image width={1000} height={1000} src={post.image}/>
                        <p>Price: {post.price}.-</p>
                        <p>Description: {post.description}</p>
                        <Link href={`/posts/${post.id}`} passHref>
                            <a>Read more</a>
                        </Link>
                    </div>
                )
            })
        }
    </div>
)

}

I have a db.json file that i connected with an API File. In this Api File i made all the fetch stuff. This shouldnt be the problem. I think the problem is, that the filter doesnt work properly, with the titels.

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

0

You are correct, JavaScript filter does not return specific property values, but it returns the top entries of the array, a.k.a posts. So return post.title or return true will yield the same result. However, the problem in your code appears to be that you are not returning anything from the map function. All you need to do is to change it to the following:

.map(post => post.title)
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