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

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

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