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

455
Visualizações
Cannot query array param from url

How to find the param whose key is an array using URLSearchParams?

// Browser url:
const url = http://localhost:4000/leads?status%5B0%5D=1&page=2;

// What my code does:
const urlParams = new URLSearchParams(window.location.search);
const myParam = (urlParams.get('status%5B0%5D') || urlParams.get(key)) || "";

// Let's focus on the below myParam:
urlParams.get('status%5B0%5D') // or
urlParams.get('status[]') // or
urlParams.has('status%5B0%5D') // or
urlParams.has('status[]') // or
urlParams.getAll('status%5B0%5D') // or
urlParams.getAll('status[]')

...has nothing useful. The aim is to delete a key from the url. Keys can be a string, status or an array, status%5B0%5D. I'm writing a function that can find the key whether it's a string or an array. How comes whatever I do has no impact? Am I missing something?

I've looked at the below and nothing works:

// Copyright (c) Microsoft Corporation. All rights reserved.
// ...

interface URLSearchParams {
    /** Appends a specified key/value pair as a new search parameter. */
    append(name: string, value: string): void;
    /** Deletes the given search parameter, and its associated value, from the list of all search parameters. */
    delete(name: string): void;
    /** Returns the first value associated to the given search parameter. */
    get(name: string): string | null;
    /** Returns all the values association with a given search parameter. */
    getAll(name: string): string[];
    /** Returns a Boolean indicating if such a search parameter exists. */
    has(name: string): boolean;
    /** Sets the value associated to a given search parameter to the given value. If there were several values, delete the others. */
    set(name: string, value: string): void;
    sort(): void;
    /** Returns a string containing a query string suitable for use in a URL. Does not include the question mark. */
    toString(): string;
    forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you are able to control the URL you can write a URL like this:

some-url?status=status1&status=status2&status=status3

Then you can do .getAll('status') which returns an array.

about 4 years ago · Juan Pablo Isaza Relatório

0

Looks like you've got to decode the URI before using it

var url = new URL(decodeURI('http://localhost:4000/leads?status%5B0%5D=1&page=2;'));

console.log(url.search);
// '?status[0]=1&page=2;'

console.log(url.searchParams.get('status[0]'))
// '1'

console.log(url.searchParams.getAll('status[0]'))
// ['1']

Array.from(url.searchParams.entries()).forEach( console.log )
// ['status[0]', '1']
// ['page', '2;']
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