高频面试题 day8
- 面试题 http://item.taobo.com/item.html?a=1&b=2&c=&d=xxx&e,请写
一段js程序提取url中各个get参数(参数名和参数个数不确定),将其key-value
形式返回到一个json结构中,如{a:“1”,b:“2”,c:“”,d:“xxx”,e:undefined}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var str = " http://item.taobo.com/item.html?a=1&b=2&c=&d=xxx&e"
var arr = {};
str = str.split("?")[1];
console.log(str);
var getArr = str.split("&");
console.log(getArr);
for(var i=0;i<getArr.length;i++){
var array= getArr[i].split("=");
arr[array[0]]=array[1];
}
console.log(arr);
</script>
</body>
</html>