Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!