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

160
Views
filtering through array of strings doesn't include the first character

I have a search bar that filters through an array containing one object:


const [termList, setTermList] = useState([{term_name: 'Name', definition: 'This is a glossary term'}]);

const filtered = termList.filter((term) =>
    Object.values(term).some(
      (val) =>
        typeof val === "string" &&
        term.term_name.includes(searched.toLowerCase())
    )
  );

return (
    <div>
        <input
          type="text"
          value={searched}
          onChange={(ev) => setSearched(ev.target.value)}
        />
      </div>
      <div className="termList">
        {filtered.map((term) => (
            <div>
              <div>{term.term_name.toLowerCase()}</div>
              <div>{term.definition}
            </div>
        ))}
    </div>
  );

When I filter through the list, however, if I type into the search bar the letter 'n', my filtered list is empty even though the only object inside the array has a name that begins with an 'n'.

Even more odd is that when I type into the search bar 'a', 'am', or 'ame' my filtered list includes the object.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are making you searched lowercase but not term.term_name, 'n' is not in 'Name', 'N' is. You need to do

term.term_name.toLowerCase().includes(searched.toLowerCase())
about 4 years ago · Juan Pablo Isaza Report

0

you can also do like this, instead of making filtered function

const [termList, setTermList] = useState([{term_name: 'Name', definition: 'This is a glossary term'}]);

return (
    <div>
        <input
          type="text"
          value={searched}
          onChange={(ev) => setSearched(ev.target.value)}
        />
      </div>
      <div className="termList">
        {termList.filter(item => (search ? item.term_name?.toLowerCase().includes(searched.toLowerCase()) : true)).map((term) => (
    <div>
      <div>{term.term_name.toLowerCase()}</div>
      <div>{term.definition}
    </div>
))}
    </div>
  );
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!