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

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

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 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