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

204
Visualizações
Serialise an Object into a list of URL query parameters?

I needed to convert an object to query parameters

I have used the code suggested here for the recursive function

const serializeObject = (obj, label)  => {
    const pairs = [];
    for (const prop in obj) {
        if (!obj.hasOwnProperty(prop)) {
            continue;
        }

        if (typeof obj[prop] === 'object') {
            pairs.push(serializeObject(obj[prop]), prop);
            continue;
        }


        if (label !== undefined) {
            pairs.push(
                "pageSearch[" + label + "][" + prop + "]" + "=" + obj[prop]
            );
        } else {
            pairs.push("pageSearch[" + prop + "]" + "=" + obj[prop]);
        }

    }
    return pairs.join("&");
};

I have added the 2nd parameter to the functions because I needed parameters in this format pageSearch[li_date][datefrom] and pageSearch[li_date][dateto]

But the 2nd parameter label always remains undefined. Please suggest what's wrong here?

JavaScript Object for testing the function:

{
"pageSearch": {
    "id": "",
    "agent_name": "",
    "access": "",
    "li_date": {
        "datefrom": "04/28/2022 02:15 PM",
        "dateto": "04/28/2022 02:15 PM"
    },
    "email": "",
    "phone": "",
    "date": {
        "datefrom": "04/28/2022 02:15 PM",
        "dateto": "04/28/2022 02:15 PM"
    },

}

}

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

0

Why don't you just do

url = "http//someurl/?obj=" + encodeURIComponent(JSON.stringify(myObject));

and on the other side you decode it like this:

const myObj = JSON.parse(new URLSearchParams(window.location.search).get("obj"))
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