Answer the question
In order to leave comments, you need to log in
How to serialize nested object?
I want to translate the nested object into a string to send to the backend.
https://jsfiddle.net/rwcjapft/
There is such an object
let obj = {
page: "1",
search: {title: "gfsg", without_complex: false},
size: "10",
items: 323
}
page=1&search[title]=gfsg&search[without_complex]=false&size=10&items=323
Answer the question
In order to leave comments, you need to log in
const serialize = (obj, path) =>
Object.entries(obj).map(([ k, v ]) => {
const p = path ? `${path}[${k}]` : k;
return v instanceof Object ? serialize(v, p) : `${p}=${v}`;
}).join('&');
There is a ready-made solution for this: https://github.com/ljharb/qs
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question