room_number[]=2&room_number[]=3&room_number[]=4&price_min=111&price_max=999&area[]=Ленинский район
[
{name: 'room_number_2', value: 2},
{name: 'room_number_3', value: 3},
{name: 'room_number_4', value: 4},
{name: 'price_min', value: 111},
{name: 'price_max', value: 999},
{name: 'area', value: 'Ленинский район'},
]
const queryString =
"room_number[]=2&room_number[]=3&room_number[]=4&price_min=111&price_max=999&area[]=Ленинский район"
function convertQueryString(query) {
const result = []
const pairs = query.split("&")
pairs.forEach((pair) => {
const [name, value] = pair.split("=")
const cleanName = name.replace(/\[\]/g, "")
result.push({
name: cleanName,
value: decodeURIComponent(value),
})
})
return result
}
const output = convertQueryString(queryString)
console.log(output)
const queryString = "room_number[]=2&room_number[]=3&room_number[]=4&price_min=111&price_max=999&area[]=Ленинский район";
const output = queryString.split('&').map(pair => {
const [name, value] = pair.split('=')
return {
name: name.replace(/\[\]/g, ''),
value: decodeURIComponent(value),
}
})
console.log(output)
function parseQueryString(data) {
const result = {};
const pairs = data.split('&').map(x=>x.split('='));
for(const [name, value] of pairs) {
const clean_name = name.endsWith('[]') ? name.substr(0, name.length - 2) : name;
if(!result.hasOwnProperty(clean_name)) {
result[clean_name] = [];
}
result[clean_name].push(value);
}
return result;
}
{
area: ["Ленинский район"],
price_max: ["999"],
price_min: ["111"],
room_number: ["2", "3", "4"]
}