urlParams = objectToQuery({
page: {
num: 1,
size: 10
},
filter: {
class: {
id: 1,
name: '11212'
},
name: 'xxx'
}
})
console.log(urlParams)
?page[num]=1&page[size]=10&filter[class][id]=1&filter[class][name]=11212&filter[name]=xxx
/**
* @description: 把object转化为url请求参数
* @param {type}
* @return: 拼接url
*/
objectToQuery() {
let obj = arguments[0];
let prefix = arguments[1];
if (typeof obj !== "object") return "";
const attrs = Object.keys(obj);
return attrs.reduce((query, attr, index) => {
// 判断是否是第一层第一个循环
if (index === 0 && !prefix) query += "?";
if (typeof obj[attr] === "object") {
const subPrefix = prefix ? `${prefix}[${attr}]` : attr;
query += this.objectToQuery(obj[attr], subPrefix);
} else {
if (prefix) {
query += `${prefix}[${attr}]=${obj[attr]}`;
} else {
query += `${attr}=${obj[attr]}`;
}
}
// 判断是否是第一层最后一个循环
if (index !== attrs.length - 1) query += "&";
return query;
}, "");
}
转载请注明出处
网友评论