demoFlow=sms&items.0.name=iPhone 11&items.0.price=100000&items.0.quantity=1&items.1.name=Чехол&items.1.price=500&items.1.quantity=1&promoCode=default&sum=100500
const obj = {
demoFlow: 'sms',
promoCode: 'default',
sum: 100500,
items: [
{
name: 'iPhone 11',
quantity: 1,
price: 100000,
},
{
name: 'Чехол',
quantity: 1,
price: 500,
},
]
}
const toString = (val, keys = []) =>
val instanceof Object
? Object.entries(val).map(([ k, v ]) => {
keys.push(k);
const result = toString(v, keys);
keys.pop();
return result;
}).join('&')
: `${keys.join('.')}=${val}`;
function toString(val) {
const result = [];
for (const stack = [ [ val, [] ] ]; stack.length;) {
const [ n, keys ] = stack.pop();
if (n instanceof Object) {
stack.push(...Object.entries(n).map(([ k, v ]) => [ v, [ ...keys, k ] ]).reverse());
} else {
result.push(`${keys.join('.')}=${n}`);
}
}
return result.join('&');
}