//解析url为对象格式
// 'www.u.com/home?id=2&type=0&dtype=-1'
let url = 'www.u.com/home?id=2&type=0&dtype=-1';
function urlEdit(url){
let urla = url.split("?"); //["www.u.com/home","id=2&type=0&dtype=-1"] 以问号为分隔符
let urlb = urla[1].split("&"); //["id=2","type=0","dtype=-1"]
let urlc = []; //[[id,2],[type,0],[dtype,-1]]
let json = {}; //{id: "2", type: "0", dtype: "-1"}
for(var i = 0; i < urlb.length; i++){
urlc.push(urlb[i].split("=")); //以等号为分隔符,分成字符串数组
}
let urld = JSON.stringify(urlc);
for(var i = 0; i < urlc.length; i++){
for(var j = 0; j <urlc[i].length; j++){
// console.log(urlc[i][j]);
name = urlc[i][0];
value = urlc[i][1];
json[name] = value; //添加进对象
}
}
return json
}
urlEdit(url); //调用 传入要解析的url
网友评论