js对url参数解析,返回一个对象

var url = 'www.imooc.com?name=xiaodangao&age=22&sex=0&phone=19023271580';
function fetchData(url) {
	var obj = {};
	var temp;
	var paramsGroup = url.split('?')[1]; //name=QinYanFei&age=22&sex=0&phone=13083771680
	var paramsObj = paramsGroup.split('&'); // ["name=QinYanFei", "age=22", "sex=0", "phone=13083771680"]
	for (var index of paramsObj) { //for in是遍历(object)键名,for of是遍历(array)键值。
		console.log(index) //name=QinYanFei,age=22...
		temp = index.split('='); // 依次["name", "QinYanFei"]  ["age", "22"]  ["sex", "0"] ["phone", "13083771680"]
		obj[temp[0]] = temp[1]; //键值:键名
	}
	return obj;
}
fetchData(url)//{name: "xiaodangao", age: "22", sex: "0", phone: "13083771680"}
上一篇:Python中的类和JavaScript中的类(ES6)


下一篇:统计字段等于不同值的数量