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

175
Views
How can I make this map filter shorter & better

I think this is not an ideal way of doing filters. I have to type more if else's and it doesn't look good.

I found a package called "useFilter". but can I just make it without using any packages? like with hooks or something.

 
import { AddPosition } from "./../../data/careers/AddPosition";

const Positions = () => {
  const [positions] = useState(AddPosition);
  const [searchTerm, setSearchTerm] = useState("");

   <input
                type="search"
                placeholder="Search"
                onChange={(e) => setSearchTerm(e.target.value)}
              />
             
                   {positions
            .filter((position) => {
              if (searchTerm === "") {
                return position;
              } else if (
                position.category
                  .toLowerCase()
                  .includes(searchTerm.toLowerCase())
              ) {
                return position;
              } else if (
                position.type.toLowerCase().includes(searchTerm.toLowerCase())
              ) {
                return position;
              } else if (
                position.location
                  .toLowerCase()
                  .includes(searchTerm.toLowerCase())
              ) {
                return position;
              } else if (
                position.position
                  .toLowerCase()
                  .includes(searchTerm.toLowerCase())
              ) {
                return position;
              }
            })
            .map((position) => (
              <>
                <SinglePosition
                  id={position.id}
                  category={position.category}
                  type={position.type}
                  position={position.position}
                />
              </>
            ))}
        </div>
      </div>
    </div>
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You can simplify this by using Array.prototype.some and you can also use useMemo to make sure that the filtering happens only when positions/searchTerm changes.

const filteredPositions = useMemo(() => {
      if(!searchTerm) return positions;
      const lowerCasedSearchTerm = searchTerm.toLowerCase();
      return positions.filter(({id, ...other}) => {
          // assuming you want to check every value in position
          return Object.values(other).map(v => v.toLowerCase()).some(v => v.includes(lowerCasedSearchTerm))
      })
  }, [positions, searchTerm])

Now you can map filteredPositions in the JSX.

about 4 years ago · Santiago Gelvez Report

0

You're logic is repeated for each property of position. Combine them to a single array, and iterate it with Array.some() if any of the includes the term, it would return true:

{positions
  .filter(({ id, ...rest }) => {
    const term = searchTerm.toLowerCase();

    return !term || Object.values(rest).some(str =>
      str.toLowerCase().includes(term)
    );
  })
  .map(position => (
    <SinglePosition key={position.id} {...position} />
  ))
}

Note: when rendering a list of items don't forget to include a key property. In addition, you don't need to wrap a single element in a fragment. You need a fragment only if you render multiple elements without a single parent.

about 4 years ago · Santiago Gelvez 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!