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

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

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