Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
¿Serializar un objeto en una lista de parámetros de consulta de URL?

Necesitaba convertir un objeto en parámetros de consulta.

He usado el código sugerido aquí para la función recursiva.

 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("&"); };

He agregado el segundo parámetro a las funciones porque necesitaba parámetros en este formato pageSearch[li_date][datefrom] y pageSearch[li_date][dateto]

Pero la label del segundo parámetro siempre permanece sin definir. Por favor, sugiera lo que está mal aquí?

Objeto JavaScript para probar la función:

 { "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 answers
Answer question

0

¿Por qué no simplemente haces

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

y por otro lado lo decodificas así:

 const myObj = JSON.parse(new URLSearchParams(window.location.search).get("obj"))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!