Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda