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

194
Visualizações
remove specific query string react-router-dom v6

Hi I have an array of a query string with the same key and I wanna remove one of them:

https://example.com/something?orderby=date&orderby=price&orderby=amount

  const [searchParams, setSearchParams] = useSearchParams();
  let udpatedSearchParams = new URLSearchParams(searchParams.toString());

  const remove = (key) => {
    udpatedSearchParams.delete(key);
    setSearchParams(udpatedSearchParams.toString());
  };

remove("orderby") // => https://example.com/something?

but I wanna this:

remove("orderby","amount") // => https://example.com/something?orderby=date&orderby=price

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It doesn't have the fanciest variable naming but it does the work

const remove = (key, url) => {
  let reqUrl = url.split("?");
  let paramArray = reqUrl[1].split("&");
  let result = paramArray.reduce(function (a, b) {
    console.log(b);
    if (b.split("=")[1] !== key) {
      return [...a, b];
    } else {
      return [...a];
    }
  }, []);

  return reqUrl[0] + "?" + result.join("&");
};

remove(
  "price",
  "https://example.com/something?orderby=date&orderby=price&orderby=amount"
);

output will be:

'https://example.com/something?orderby=date&orderby=amount'
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