将字符串转换成对象,重要
1 // http://www.baidu.com?username=xiaowang&password=12345&age=18&sex=女&score=90 2 // {username:"xiaowang",password:12345,age:18,sex:"女",score:90} 3 4 var url = "http://www.baidu.com?username=xiaowang&password=12345&age=18&sex=女&score=90"; 5 var obj = {}; 6 var strArr = url.split("?"); 7 // console.log(strArr[1]);//username=xiaowang&password=12345&age=18&sex=女&score=90 8 var strArr2 = strArr[1].split("&"); 9 // console.log(strArr2);//["username=xiaowang", "password=12345", "age=18", "sex=女", "score=90"] 10 for (var i = 0; i < strArr2.length; i++) { 11 // strArr2[i].split("=")//["username","xiaowang"] 12 obj[strArr2[i].split("=")[0]] = strArr2[i].split("=")[1]; 13 } 14 console.log(obj);//{username: "xiaowang", password: "12345", age: "18", sex: "女", score: "90"} 15 16 17 18 // var str = 'username=xiaowang&password=12345&age=18&sex=女&score=90' 19 function queryString(str) { 20 var obj = {}; 21 var strArr2 = str.split("&"); 22 for (var i = 0; i < strArr2.length; i++) { 23 obj [ strArr2[i].split("=")[0] ] = strArr2[i].split("=")[1]; 24 } 25 return obj; 26 }