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

153
Views
return empty string if condition isn't met

I want to make the request filter option for location or/and sensorType return an empty string if any of them is null.

I'm using a select dropdown to change the values of filter.location and filter.sensorType

Expected output:

  1. if filter.location is null but filter.sensorType is not null
    request = ...farms?sensortype=abc&page=3
  2. if filter.location is not null but filter.sensorType is null
    request = ...farms?location=abc&page=3
  3. if both are null
    requests = ...farms?page=3

My current result:

...farms?location=abc&undefined&page=3

const [filter, setFilter] = useState({
  sensorType: null,
  location: null,
});


// API REQUEST
const {
  data
} = await axios.get(
  `http://localhost:8080/api/farms?${
          filter.location && `location=${filter.location}`
        }${filter.sensorType && `&sensorType=${filter.sensorType}`}&page=${page}`
);

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

0

Just use the ternary conditional operator:

condition ? resultIfTruthy : resultIfFalsy

const [filter, setFilter] = useState({
  sensorType: null,
  location: null,
});


// API REQUEST
const {
  data
} = await axios.get(
  `http://localhost:8080/api/farms?${
          filter.location ? `location=${filter.location}` : ''
        }${filter.sensorType ? `&sensorType=${filter.sensorType : ''}`}&page=${page}`
);

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!