解析url

作者: Augenstern___ | 来源:发表于2018-08-24 10:55 被阅读0次

//解析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

相关文章

网友评论

      本文标题:解析url

      本文链接:https://www.haomeiwen.com/subject/wokbiftx.html