js converts the information in the url address into an object obj

js converts the information in the url address into an object obj

function urlToObj(str) {
    
    
    let obj = {
    
    };
    let str1 = str.split("?");
    let str2 = str1[1].split("&");  //["a=1", "b=2", "c=3"]
    for (let i = 0; i <str2.length ; i++) {
    
    
        let str3 = str2[i].split("=");
        obj[str3[0]] = str3[1]
    }
    return obj
}
var url = "http://www.baidu.com?a=1&b=2&c=3" ;
urlToObj(url)

Guess you like

Origin blog.csdn.net/qq_42208679/article/details/104246099